SK1990 Posted May 16, 2013 Share Posted May 16, 2013 I'm trying to embed a sql query into my php on my html page. I keep getting the error mysql_fetch_array() expects parameter 1 to be resource, boolean is given. What am I doing wrong? (Please note I'm not well versed in PHP and need specific directions on how to change my code) <?php require_once('auth.php'); ?> <html> <!-- This is the stub page for tying in your final project database into the website --> <!-- Don't forget to add premissions to your final project database so that your user can access it --> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Final Project</title> </head> <body> <h1>Final project page <?php echo $_SESSION['SESS_FIRST_NAME'];?></h1> <p>This is where you will link in your final project database</p></br> <p>Good luck!</p></br> <div id="innerWrapper"> <!--start of sql embedding --> <a href="index.php">Login Page</a> | <a href="amenu.php">Menu Page</a> | <a href="logout.php">Logout</a> <h2>Customer Contact Info:</h2> <?php //Verified passwords $vlogin=$_SESSION['vlogin']; $vpassword=$_SESSION['vpasswd']; //This is the connection string for the database, server, username and password $con = mysql_connect("localhost",$vlogin,$vpassword); //CASE SENSITIVE if (!$con) { die('Could not connect: ' . mysql_error()); } //This is the connection string for the database, server, username and password $con = mysql_connect("localhost",$vlogin,$vpassword); //CASE SENSITIVE if (!$con) { die('Could not connect: ' . mysql_error()); } // Actually using the connection string and connecting to the database like the 'use' command mysql_select_db("finalprojectdb", $con); //The actual SQL code that will return a table structure into the local variable $result //Important note, PHP variables are case sensitive $result = mysql_query("SELECT Cust_Num, Contact_Name, Contact_Phone from Customer order by Cust_Num"); //constructing the table and column names //ECHO allows HTML to go through PHP to the server echo "<table border=''> <tr> <th>Customer Number</th> <th>Contact Name</th> <th>Contact Phone</th> </tr>"; // Looping until there are no more records from $result //If there are records, print the colum for that row //also ECHO allows HTML to pass through PHP while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['CUST_NUM'] . "</td>"; echo "<td>" . $row['CONTACT_NAME'] . "</td>"; echo "<td>" . $row['CONTACT_PHONE'] . "</td>"; echo "</tr>"; } echo "</table>"; //Closing the SQL connection string mysql_close($con); ?> </div> <div> [<a href="aMenu.php">Return to home page</a>] </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/ Share on other sites More sharing options...
PravinS Posted May 16, 2013 Share Posted May 16, 2013 Please check your SQL query Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430354 Share on other sites More sharing options...
SK1990 Posted May 16, 2013 Author Share Posted May 16, 2013 I did, I don't see anything wrong. do you? Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430356 Share on other sites More sharing options...
PravinS Posted May 16, 2013 Share Posted May 16, 2013 SQL syntax is fine, but check the table name, field names etc. Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430357 Share on other sites More sharing options...
SK1990 Posted May 16, 2013 Author Share Posted May 16, 2013 it's throwing the error on line 76.. which is this line while($row = mysql_fetch_array($result)) Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430359 Share on other sites More sharing options...
SK1990 Posted May 16, 2013 Author Share Posted May 16, 2013 customer was spelt with a small c in the database, i had it as a capital C..i changed it to lowercase and uploaded.. still no such luck Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430360 Share on other sites More sharing options...
PravinS Posted May 16, 2013 Share Posted May 16, 2013 echo the query and execute it in MySQL and check the result Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430362 Share on other sites More sharing options...
SK1990 Posted May 16, 2013 Author Share Posted May 16, 2013 it executes perfect in sql. Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430363 Share on other sites More sharing options...
PravinS Posted May 16, 2013 Share Posted May 16, 2013 what version of php you are using Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430365 Share on other sites More sharing options...
Barand Posted May 16, 2013 Share Posted May 16, 2013 try $result = mysql_query("SELECT Cust_Num, Contact_Name, Contact_Phone from Customer order by Cust_Num"); if (!$result) die(mysql_error()); Post any error message that it outputs Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430395 Share on other sites More sharing options...
SK1990 Posted May 16, 2013 Author Share Posted May 16, 2013 no database selected Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430458 Share on other sites More sharing options...
SK1990 Posted May 16, 2013 Author Share Posted May 16, 2013 I don't know what version of php sorry, the instructor gave the php template to work off of so all the php is his. This is for an SQL class. Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430461 Share on other sites More sharing options...
SK1990 Posted May 16, 2013 Author Share Posted May 16, 2013 now i get this: Â SELECT command denied to user 'test@localhost' for table 'customer' Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430466 Share on other sites More sharing options...
SK1990 Posted May 16, 2013 Author Share Posted May 16, 2013 Ok, I added the databasename.table and that helped.. and granted the user priviledges.. now everything shows up except the data fields in the table Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430468 Share on other sites More sharing options...
gristoi Posted May 17, 2013 Share Posted May 17, 2013 do some error checking, the error you are getting is because the query could not successfully execute, hence no resource. try this: $result = mysql_query("SELECT Cust_Num, Contact_Name, Contact_Phone from Customer order by Cust_Num") or die(mysql_error()); //that will tell you what is wrong with your query Quote Link to comment https://forums.phpfreaks.com/topic/278052-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-is-given/#findComment-1430638 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.