Pain Posted October 10, 2011 Share Posted October 10, 2011 Hello guys. Trying to connect php with mysql database and then display results on the screen. This is my code: <?php $dbhost = "localhost"; $dbuser = "username1"; $dbpass = "password1"; $db = "username1_myDB"; $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Could not connect"); mysql_select_db($connection, $db); $show = "SELECT Name, Description FROM people"; $result = mysql_query($show); while($show = mysql_fetch_array($result)){ $field01 = $show[Name]; $field02 = $show[Description]; echo "id: $field01<br>"; echo "description: $field02<p>"; } ?> However im getting this: Warning: mysql_select_db() expects parameter 1 to be string, resource given in /home/pain33/public_html/index.php on line 20 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/pain33/public_html/index.php on line 26 Any ideas how to fix this? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/248838-mysql-database-connection/ Share on other sites More sharing options...
Destramic Posted October 10, 2011 Share Posted October 10, 2011 looks like you've got your parameters mixed up...try: mysql_select_db($connection, $db); Quote Link to comment https://forums.phpfreaks.com/topic/248838-mysql-database-connection/#findComment-1277891 Share on other sites More sharing options...
Pain Posted October 10, 2011 Author Share Posted October 10, 2011 fixed that. Now it doesnt show anything. Just a blank page Quote Link to comment https://forums.phpfreaks.com/topic/248838-mysql-database-connection/#findComment-1277895 Share on other sites More sharing options...
jcbones Posted October 10, 2011 Share Posted October 10, 2011 Add the following, right under the opening <?php tag error_reporting(-1); ini_set('display_errors',1); Quote Link to comment https://forums.phpfreaks.com/topic/248838-mysql-database-connection/#findComment-1277897 Share on other sites More sharing options...
Pain Posted October 10, 2011 Author Share Posted October 10, 2011 nothing changed Quote Link to comment https://forums.phpfreaks.com/topic/248838-mysql-database-connection/#findComment-1277902 Share on other sites More sharing options...
jcbones Posted October 10, 2011 Share Posted October 10, 2011 Run this: <?php $dbhost = "localhost"; $dbuser = "username1"; $dbpass = "password1"; $db = "username1_myDB"; $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Could not connect"); mysql_select_db($connection, $db); $show = "SELECT Name, Description FROM people"; $result = mysql_query($show) or trigger_error(mysql_error()); while($show = mysql_fetch_array($result)){ $field01 = $show[Name]; $field02 = $show[Description]; echo "id: $field01<br>"; echo "description: $field02<p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248838-mysql-database-connection/#findComment-1277935 Share on other sites More sharing options...
darkspire Posted October 11, 2011 Share Posted October 11, 2011 heres what I use. //mySQL.php <?php require_once('util.php'); define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASSWORD', ''); define('DB_DATABASE', 'illumina_rpg'); $con = ""; function connect_mySQL() { global $con; $con = mysql_connect( DB_HOST, DB_USER, DB_PASSWORD); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db( DB_DATABASE, $con); return $con; } function close_mySQL() { global $con; mysql_close($con); } function write_mySQL( $query ) { global $con; if (!mysql_query($query,$con)) die('Error: ' . mysql_error()); } function read_mySQL( $query ) { return mysql_query($query); } Quote Link to comment https://forums.phpfreaks.com/topic/248838-mysql-database-connection/#findComment-1277989 Share on other sites More sharing options...
codefossa Posted October 11, 2011 Share Posted October 11, 2011 Your mysql_select_db() function had the parameters backwards. Also, inside your while() loop, you had $array[key] when it needs to be $array['key']. <?php $dbhost = "localhost"; $dbuser = "username1"; $dbpass = "password1"; $db = "username1_myDB"; $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to connect to MySQL server."); mysql_select_db($db, $connection); $show = "SELECT Name, Description FROM people"; $result = mysql_query($show); while ($show = mysql_fetch_array($result)) { $field01 = $show['Name']; $field02 = $show['Description']; echo "id: {$field01}<br>"; echo "description: {$field02}<p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248838-mysql-database-connection/#findComment-1277990 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.