Jump to content

Warning: Mysql_Fetch_Array() Expects Parameter 1 To Be Resource, Boolean Given In


Recommended Posts

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 by EzwanAbid

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, & Suggestions

This is NOT a help forum! Do not post topics asking for help not related to the website.

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.

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
}

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 :confused: :-\

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 by SocialCloud

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);

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 by EzwanAbid

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.. :o

Edited by EzwanAbid

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);
?>

@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 by EzwanAbid

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/>';
   }

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.

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 ! :happy-04:

 

@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. :happy-04:

 

Thank you again all ! :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.