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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Aha! That's what I was after!

That threw up the Call to undefined function mysql_connect()error which I've seen floating rond in several FAQs so I can get on to it.

 

Thanks for that PFMaBismAd.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.