PHP/MySQL Connection Nightmare

On my ISP’s server is installed:

MySQL 5.5.12
PHP 5.2.17

Via phpMyAdmin I setup a simple test database. Now I want to do a test access of the data using a PHP script.

My ISP said the server path to my test database (john_test) is: /var/lib/mysql/

And that my access to my test database ‘john_test’ would be: /var/lib/mysql/john_test

It appears I’m not doing something right, because I cannot connect with the following script:


<?PHP

//connect to server 

$connect = mysql_connect("/var/lib/mysql/john_test", "john", "my_real_password_goes_here");
if (!$connect) {
    die('Not connected : ' . mysql_error());
}

//connect to datatbase

$db_selected = mysql_select_db("john_test", $connect);
if (!$db_selected) {
    die ('Can\\'t use john_test : ' . mysql_error());
}


//query the database

$query = mysql_query("SELECT * FROM table1 WHERE firstname = 'John' ");

//fetch the results / convert results into an array

    WHILE($rows = mysql_fetch_array($query)):

       $firstname = $rows['firstname'];
       $lastname = $rows ['lastname'];

    echo "$firstname<br>";
    echo "$lastname<br>";

    endwhile;

?>

I’m also a bit suspicious of not using { } 's with the WHILE loop instead of just the “:” as seen in some other code.

Any assistance would be appreciated !

Thanks very much.

-GoingBONKERS

Before you get any further, please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.

Has the user “John” been given permission to access the database?

^ what he says
And also: what error do you get?

The path looks wrong for a socket connection + your missing the socket indicator. ( : or .sock )

Try using these two examples:
localhost
:/var/lib/mysql/john_test

Try them one by one, and see if the “Not connected” error is still thrown.

Though as mentioned, you should consider moving over to PDO or mysqli_.

In regards to the while, that is an alternative syntax for control structures. The key is that it starts with “:” and then a few lines under you have “endwhile;”. Which one you use is your choice, but be consistent. The way we do it is use { } in the code layer, and the : endif; etc. in the template code.

You can read more about it here: http://www.php.net/manual/en/control-structures.alternative-syntax.php

Thanks for the replies and info. Got re-tasked here with a few Murphy Strikes and finally getting back to this.

As it turns out, my ISP should have said to use ‘127.0.0.1’ instead of /var/lib/mysql/john_test {SIGH}.

The initial test (with some code revisions) is now working.

I appreciated the responses and suggestions.

-GoingBONKERS