Jump to content

[SOLVED] MySQL? No connection


ChrisDN

Recommended Posts

If you could just bare with a PHP/MySQL newbie for a second...

 

 

MySQL version: 5.0

PHP version: 5.2.6

 

$link = mysql_connect("localhost", "user", "password");
if (!$link) {
    die("Could not connect: " . mysql_error());
}
echo "Connected successfully";
mysql_close($link);

 

mysql_error() returns nothing.

 

 

 

Quite simply nothing happens; the browser window is blank.

phpinfo(); shows PHP to be running fine and I can run standard PHP scripts.

I can log in to MySQL via the command line and through HeidiSQL to do everything. I just can't seem to connect to the database via PHP to do anything.

 

Being as this is my first time venturing into PHP + MySQL I haven't really tried much else apart from uninstalling Apache, PHP + MySQL and starting from scratch following each installation step to the letter; still no joy.

I presume I've not quite set up MySQL correctly somewhere.

 

What could I be missing?

Link to comment
https://forums.phpfreaks.com/topic/108816-solved-mysql-no-connection/
Share on other sites

I think you should use new mysqli php extension instead of old mysql

Thanks, I shall give that a try.

 

Your only connecting to mysql, but you also then need to select the db you want to use...

I know how to connect to the database and had been doing that previously but it wasn't working. I found the script/query I posted just to test the connection.

When display_errors are off or the error_reporting level is not set to show all errors an undefined mysql_connect() function will execute the or die() statement but mysql_error() will not exist either.

 

Add the following two lines after your first opening <?php tag -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

This will show fatal runtime, warning, and notice error messages (fatal parse errors won't be shown by this unless you set these two equivalent values in your php.ini or a .htaccess file.)

 

When learning php, developing php code, or debugging php code, always turn on full php error reporting to get php to help you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.