a09hopper Posted July 27, 2016 Share Posted July 27, 2016 <?php $database = array(); $database['host'] = "localhost"; $database['port'] = '3306'; $database['name'] = "forumtest"; $database['username'] = "root"; $database['password' = "Password"; $link = mysql_connect($database['host], $database['username'], $database['password']); if ($link) { echo "Connected to a database".$database['name']; }else{ echo connect to a database"$database['name'] . "failed<br/>"; echo "Error: ".mysql_error(); } ?> This is my code for a test to connect to a database however, there seems to be a problem with the code as when I try to connect it just gives me a error 500. I would appreciate any help. Quote Link to comment https://forums.phpfreaks.com/topic/301655-php-wont-connect-to-mysql-databse/ Share on other sites More sharing options...
benanamen Posted July 27, 2016 Share Posted July 27, 2016 (edited) Toss that code in the trash. It has been obsolete for over a decade and is now completely removed from PHP. You need to use PDO. Edited July 27, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301655-php-wont-connect-to-mysql-databse/#findComment-1535152 Share on other sites More sharing options...
ginerjm Posted July 27, 2016 Share Posted July 27, 2016 And don't ever post REAL credentials on the internet! Plus - why use an array when you have the values handy? Quote Link to comment https://forums.phpfreaks.com/topic/301655-php-wont-connect-to-mysql-databse/#findComment-1535160 Share on other sites More sharing options...
cyberRobot Posted July 27, 2016 Share Posted July 27, 2016 (edited) You are missing some quotes and other components. You could try the following: <?php $database = array(); $database['host'] = "localhost"; $database['port'] = '3306'; $database['name'] = "forumtest"; $database['username'] = "root"; $database['password'] = "Password"; $link = mysql_connect($database['host'], $database['username'], $database['password']); if ($link) { echo "Connected to a database".$database['name']; }else{ echo "connect to a database" . $database['name'] . "failed<br/>"; echo "Error: ".mysql_error(); } ?> With that said, the mysql_* functions were removed in the newest version of PHP, as benanamen was hinting at. More information can be found here: http://php.net/manual/en/mysqlinfo.api.choosing.php Edited July 27, 2016 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/301655-php-wont-connect-to-mysql-databse/#findComment-1535164 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.