ChrisDN Posted June 5, 2008 Share Posted June 5, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/108816-solved-mysql-no-connection/ Share on other sites More sharing options...
luca200 Posted June 5, 2008 Share Posted June 5, 2008 I think you should use new mysqli php extension instead of old mysql Quote Link to comment https://forums.phpfreaks.com/topic/108816-solved-mysql-no-connection/#findComment-558215 Share on other sites More sharing options...
rarebit Posted June 5, 2008 Share Posted June 5, 2008 Your only connecting to mysql, but you also then need to select the db you want to use... $conn = mysql_connect($host, $user, $pass) or die(mysql_error()); mysql_select_db($db, $conn) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/108816-solved-mysql-no-connection/#findComment-558218 Share on other sites More sharing options...
ChrisDN Posted June 5, 2008 Author Share Posted June 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108816-solved-mysql-no-connection/#findComment-558250 Share on other sites More sharing options...
PFMaBiSmAd Posted June 5, 2008 Share Posted June 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108816-solved-mysql-no-connection/#findComment-558317 Share on other sites More sharing options...
ChrisDN Posted June 5, 2008 Author Share Posted June 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108816-solved-mysql-no-connection/#findComment-558359 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.