Using MySql

Discussion in 'General' started by mphayesuk, Dec 31, 2006.

  1. mphayesuk

    mphayesuk Member HowtoForge Supporter

    Hi, I have tried looking on google ect... but I cant find a definate answer.

    This is what I have:

    web4_u2 - username
    web4_db2 - database
    MainTable - table name

    I use this to try and connect from test.php

    $hostname = "www.touch-one.co.uk";
    $mysql_login = "web4_u2";
    $mysql_password = "mypassword";
    $database="web4_db2";
    $number = "04381003";

    print "<p>Data for " .$number;

    if (!($dbcon = mysql_pconnect($hostname, $mysql_login , $mysql_password))){
    die("Can't connect to database server.");
    }else{
    // select a database
    if (!(mysql_select_db("$database",$dbcon))){
    die("Can't connect to database.");
    }
    // more code commented out
    }


    All I get is "Cant connect to database server". the server is running because I can access through the portal and also through putty.

    Thanks
     
  2. Craig

    Craig New Member

    I could be wrong but I think you would not use the host/domain for the connection to the MySQL server but instead, since your scripts are on the same box as the MySQL server, that you would instead use either localhost or 127.0.0.1.

    Usually 127.0.0.1 is a safe bet to work.

    But, if you have phpMyAdmin installed and it does actually work, you could do what I do and that is cheat, take a look at phpMyAdmin's configuration file which should be, if installed as a package through ISPConfig, in
    Code:
    /home/admispconfig/ispconfig/web/phpmyadmin/config.inc.php
    Take a look in there and see what it has listed for the "$hostname" because I think it might work better with either :
    Code:
    $hostname = "127.0.0.1";
    or :
    Code:
    $hostname = "localhost";
    That's just a guess though.
     
  3. mphayesuk

    mphayesuk Member HowtoForge Supporter

    Excellent thanks that works, but if you could take a look at the enitre code I am using and let me know if there is something obviously wrong, I am very new to mysql so I am still learning.


    <html>
    <head>
    <title>Result of Database Query</title>
    </head>
    <body>
    <h1>Result of Database Query</h1>
    <?

    $hostname = "localhost";
    $mysql_login = "web4_u2";
    $mysql_password = "******";
    $database="web4_db2";
    $number = "04381003";

    print "<p>Data for " .$number;

    if (!($dbcon = mysql_pconnect($hostname, $mysql_login , $mysql_password))){
    die("Can't connect to database server.");
    }else{
    // select a database
    if (!(mysql_select_db("$database",$dbcon))){
    die("Can't connect to database.");
    }



    $sql="SELECT * FROM MainTable WHERE CompanyNumber = "04381003" ";
    $result = mysql_query($sql);
    $nrows = mysql_num_rows($result);
    if($nrows != 0)
    {
    //print "<p>Data for " . $number;
    print "<table border=2><tr><th>Latitude<th>Longitude<th>Easting<th>Northing\n";
    for($j=0;$j<$nrows;$j++)
    {
    $row = mysql_fetch_array($result);
    print "<tr><td>" . $row["Type"];
    print "<td>" . $row["IncorpDate"];
    print "\n";
    }
    print "</table>\n";
    }
    else print "<p>No Entry for " . $number;
    mysql_close($dbcon);
    }
    ?>
    </p>
    </body>
    </html>

    If I just leave in the connection statements, I dont get any errors so I am assuming there are ok, and the select statement works on the command line and returns data so I am pretty sure that works.

    Thanks
     
  4. mphayesuk

    mphayesuk Member HowtoForge Supporter

    Sorry I think I have it working now.... thanks for your help....
     

Share This Page