EzwanAbid Posted November 25, 2012 Share Posted November 25, 2012 (edited) I try to make a search box which can search and display "student_id" by entered the correct value but I keep getting this error message "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\Project\FinalProject\searchres.php on line 11". Looks like something wrong with my sql code but I have no idea how to fix it.. Hope someone can give me an advice,solution or corrections maybee.. Thank You. <?php $host_name = "localhost"; $user_name = "root"; $password = ""; $db_name = "finalproject"; $term = $_POST['term']; $sql = mysql_query("SELECT * FROM student WHERE student_id like '%$term%' or ic_number like '%$term%'"); while ($row = mysql_fetch_array($sql)){ echo 'Student ID: '.$row['student_id']; echo '<br/> Fullname: '.$row['fullname']; echo '<br/> IC No.: '.$row['ic_number']; echo '<br/> Course: '.$row['course']; echo '<br/> Type Of Letter: '.$row['type_of_letter']; echo '<br/><br/>'; } ?> Edited November 25, 2012 by EzwanAbid Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/ Share on other sites More sharing options...
requinix Posted November 25, 2012 Share Posted November 25, 2012 If mysql_query() returns a boolean for a SELECT query then it means your query failed. There was an error. 1. What is the value of $term? 2. Why aren't you escaping it? 3. Why aren't you using mysqli or PDO and prepared statements? That's the most secure way to run a query involving user input. 4. If after #1 you don't see the problem, echo out mysql_error to get an error message. Also, PHPFreaks.com Questions, Comments, & SuggestionsThis is NOT a help forum! Do not post topics asking for help not related to the website. Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1394932 Share on other sites More sharing options...
EzwanAbid Posted November 25, 2012 Author Share Posted November 25, 2012 Thank you for reply my post... Actually I just copying this code from a site and i just paste it into my Dreamweaver.. There's a html code that i didn't show it here.. i will send it here if you want to see the code.. Here the code : <html> <head> <title>Search the Database</title> </head> <body> <form action="searchres.php" method="post"> Search: <input type="text" name="term" /><br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> I only have basic PHP.. this kind of structure are new to me but I must do this for my task..and found some error there.. Thank You for your reply. Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1394941 Share on other sites More sharing options...
Andy123 Posted November 25, 2012 Share Posted November 25, 2012 Your query is failing, and you are trying to hand $sql off to mysql_fetch_array() as a resource. However, mysql_query() returns FALSE on failure, hence why $sql is FALSE. That is why you get this warning; you are giving a boolean value where a resource value is expected - exactly as the warning says. You should check if the query was executed successfully, e.g.: $sql = mysql_query("select * from mytable"); if ($sql) { // Query executed successfully if (mysql_num_rows($sql) > 0) { // At least one row returned - now we can fetch it/them while ($row = mysql_fetch_array($sql) { // Do something } } else { // No rows returned } } else { // Error executing query } Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1394946 Share on other sites More sharing options...
EzwanAbid Posted November 25, 2012 Author Share Posted November 25, 2012 Sorry Andy123... I don't get it . like I said..I'm new in PHP .. :-\ ... Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1394963 Share on other sites More sharing options...
MDCode Posted November 25, 2012 Share Posted November 25, 2012 It means your query is wrong basically. Take requinix's advice Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1394966 Share on other sites More sharing options...
EzwanAbid Posted November 25, 2012 Author Share Posted November 25, 2012 If mysql_query() returns a boolean for a SELECT query then it means your query failed. There was an error.1. What is the value of $term? 2. Why aren't you escaping it? 3. Why aren't you using mysqli or PDO and prepared statements? That's the most secure way to run a query involving user input. 4. If after #1 you don't see the problem, echo out mysql_error to get an error message. I can't understand No.2 and 3... escaping what and using mysqli or PDO and prepared statements? ermm :-\ Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1394977 Share on other sites More sharing options...
MDCode Posted November 25, 2012 Share Posted November 25, 2012 (edited) Escaping = protecting basically; use mysql_real_escape_string($_POST['term']); mysql extensions are deprecated (mysql: query, fetch_array, etc.) so using mysqli or pdo is suggested but not yet required Edited November 25, 2012 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1394983 Share on other sites More sharing options...
AyKay47 Posted November 26, 2012 Share Posted November 26, 2012 Let's focus on finding the error. Insert this snippet into your code and post the output. $sql = "SELECT * FROM student WHERE student_id like '%$term%' or ic_number like '%$term%'"; $query = mysql_query($sql) or die("Error: " . mysql_error() . "<br />In Query: " . $sql); Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1395065 Share on other sites More sharing options...
EzwanAbid Posted November 28, 2012 Author Share Posted November 28, 2012 (edited) Thank You for reply my post guys.. erm @AyKay47 , after I edit my code with your code above... i get this error message Error: Query was empty In Query: Why my Query was empty ? I have data in my phpmyadmin and I insert the correct value at the search box ($term) .... Edited November 28, 2012 by EzwanAbid Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1395819 Share on other sites More sharing options...
beyzad Posted November 28, 2012 Share Posted November 28, 2012 wow! try this: $sql = "SELECT * FROM student WHERE student_id like '%$term%' or ic_number like '%$term%'"; var_dump($sql); Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1395824 Share on other sites More sharing options...
EzwanAbid Posted November 28, 2012 Author Share Posted November 28, 2012 (edited) Thank you for you reply @beyzad , I've already try your code..some code are from @AyKay47 $sql = "SELECT * FROM student WHERE student_id like '%$term%' or ic_number like '%$term%'"; var_dump($sql); $query = mysql_query($sql) or die("Error: " . mysql_error() . "<br />In Query: " . $sql); while ($row = mysql_fetch_array($sql)){ echo 'Student ID: '.$row['student_id']; echo '<br/> Fullname: '.$row['fullname']; echo '<br/> IC No.: '.$row['ic_number']; echo '<br/> Course: '.$row['course']; echo '<br/> Type Of Letter: '.$row['type_of_letter']; echo '<br/><br/>'; } ?> ...and the result is very weird..here the output. : string(95) "SELECT * FROM student WHERE student_id like '%05-201005-18%' or ic_number like '%05-201005-18%'" Error: No database selected In Query: SELECT * FROM student WHERE student_id like '%05-201005-18%' or ic_number like '%05-201005-18%' I never see an output like this before.. Edited November 28, 2012 by EzwanAbid Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1395831 Share on other sites More sharing options...
beyzad Posted November 28, 2012 Share Posted November 28, 2012 Hi. Do not remove your first 4 lines. your code must be something like this: <?php $host_name = "localhost"; $user_name = "root"; $password = ""; $db_name = "finalproject"; $term = $_POST['term']; $sql = "SELECT * FROM student WHERE student_id like '%$term%' or ic_number like '%$term%'"; $query = mysql_query($sql) or die("Error: " . mysql_error() . "<br />In Query: " . $sql); ?> Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1395832 Share on other sites More sharing options...
EzwanAbid Posted November 28, 2012 Author Share Posted November 28, 2012 (edited) @beyzad, did you mean the code supposed to be like this? : <?php $host_name = "localhost"; $user_name = "root"; $password = ""; $db_name = "finalproject"; $term = $_POST['term']; $sql = "SELECT * FROM student WHERE student_id like '%$term%' or ic_number like '%$term%'"; $query = mysql_query($sql) or die("Error: " . mysql_error() . "<br />In Query: " . $sql); while ($row = mysql_fetch_array($sql)){ echo 'Student ID: '.$row['student_id']; echo '<br/> Fullname: '.$row['fullname']; echo '<br/> IC No.: '.$row['ic_number']; echo '<br/> Course: '.$row['course']; echo '<br/> Type Of Letter: '.$row['type_of_letter']; echo '<br/><br/>'; } ?> Here the output that i get : The database was correct, but I have no idea why it said "No database selected" Error: No database selected In Query: SELECT * FROM student WHERE student_id like '%05-201005-18%' or ic_number like '%05-201005-18%' Let me know if something wrong with this code.. Thank you for your reply. Edited November 28, 2012 by EzwanAbid Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1395839 Share on other sites More sharing options...
beyzad Posted November 28, 2012 Share Posted November 28, 2012 Hi. I dont know why i forgot this lol Add these 2 lines. mysql_connect("$host_name" , "$user_name" , "$password"); mysql_select_db("$db_name"); Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1395892 Share on other sites More sharing options...
EzwanAbid Posted November 28, 2012 Author Share Posted November 28, 2012 LOL... it's ok .. Okay now I already do as you say..and I get this error message : Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in D:\xampp\htdocs\Project\FinalProject\searchres.php on line 17 Looks like something wrong with my while statement that I don't know what is it : Here the code starting from line 17 until 24. while ($row = mysql_fetch_array($sql)){ echo 'Student ID: '.$row['student_id']; echo '<br/> Fullname: '.$row['fullname']; echo '<br/> IC No.: '.$row['ic_number']; echo '<br/> Course: '.$row['course']; echo '<br/> Type Of Letter: '.$row['type_of_letter']; echo '<br/><br/>'; } Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1395894 Share on other sites More sharing options...
beyzad Posted November 28, 2012 Share Posted November 28, 2012 Nope it is your query for sure. Do you still have this line? $query = mysql_query($sql) or die("Error: " . mysql_error() . "<br />In Query: " . $sql); Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1395901 Share on other sites More sharing options...
PFMaBiSmAd Posted November 28, 2012 Share Posted November 28, 2012 The current error is because your code changed (to get the error checking and error reporting logic in it, which you should keep btw, and should always use when forming and executing queries) and the result resource is in a variable named $query now. You would need to use that variable in your mysql_fetch_array statement. Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1395903 Share on other sites More sharing options...
EzwanAbid Posted November 29, 2012 Author Share Posted November 29, 2012 Thank You for reply my post.. By the way, thank you so much to @PFMaBiSmAd because your post bring me to the solution.. while ($row = mysql_fetch_array($query)){ after I change my code based on your post.. It's Working ! Thank you for helping this newbie ! @beyzad yes I still have the code.. but it's okay now.. I already find the solution, and my search box is fully working now. Thank you again all ! Quote Link to comment https://forums.phpfreaks.com/topic/271139-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1396114 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.