Jump to content

MySQL function expects parameter 1 to be resource, boolean given. Also "Headers already sent" error.


justsomeguy

Recommended Posts

Because so many people ask about error messages like this, and apparently don't want to figure out what the error message actually means, we'll make this a sticky topic to refer people back to when they ask about this. The error message takes this form: Warning: [some mysql function] expects parameter 1 to be resource, boolean given The function reporting the error could be any function which expects a MySQL result resource, including mysql_num_rows, mysql_fetch_array, mysql_fetch_assoc, etc. First, just to make this point: the mysql extension is old, deprecated, is missing features, and should not be used. You should be using mysqli instead, or PDO. http://www.php.net/manual/en/book.mysqli.phphttp://www.php.net/manual/en/book.pdo.php The error may be referring to a line that uses a mysql function that may look similar to this: $result = mysql_query($sql);$count = mysql_num_rows($result); The error would claim that mysql_num_rows expects a resource but got a boolean. That means that $result is a boolean value (true/false), not a MySQL result resource. So, what does that mean? The key is in the manual for mysql_query (you do look up PHP functions that you're using in the manual, right?): http://www.php.net/manual/en/function.mysql-query.php

Return ValuesFor SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
For the second group of queries, mysql_query always returns true or false to indicate if the query worked. For the first group of queries, including SELECT queries, it will return a resource if the query succeeded or boolean false if it failed. Since your error message is telling you that $result is a boolean, then that means your query failed. Like the example in the manual page for mysql_query shows, you use mysql_error to get the error message from MySQL:
$result = mysql_query($sql);if (!$result) {   die('Invalid query: ' . mysql_error());}

or, shorter: $result = mysql_query($sql) or die('Invalid query: ' .mysql_error()); Now you can figure out what the error is from MySQL, and fix your query. It will probably be useful to print out the query you're running so that you can see exactly what you're sending to MySQL.

  • Like 8
Link to comment
Share on other sites

ok now i know how to fix those errors thanks, even tho i fine this post to be harsh lol but it helped :):good:

Link to comment
Share on other sites

I don't think it's harsh at all, it's a fact that a lot of people on here see an error message and immediately post a question about it instead of trying to figure out what it means. The reason I know this is because the error message is telling them exactly what the problem is, but they still ask about it. I've also seen people post a question about an error message like this, receive the answer, and later post a question about the exact same error message with the exact same solution, which indicates that they are not reading or understanding error messages. This is one such descriptive error message, it's specifically telling you that mysql_query returned a boolean and not a resource. It's not too much to expect people to look up the manual to find out why mysql_query would return a boolean instead of posting a question in a forum. That's what professional programmers do, research. It's what I try to teach here.

Link to comment
Share on other sites

not harsh at all. the same question has been asked twice in the past two days, and is probably the most frequently asked question about an error message (if they even have errors turned on) in the PHP forum

Link to comment
Share on other sites

im kool with harshness, and on my side, i only post an error if i really dont understand it at all, like the mysql_num_rows() error, i couldnt understand that error for months now, so i avoid using that function, till dont understand it full but i got the error fixed tho, which was pretty easy after reading this topic and the doc on php.net

Link to comment
Share on other sites

Warning: Cannot modify header information - headers already sent by (output started at /home/content/14/8709614/html/***********/*****/php/config.php:11) in /home/content/14/8709614/html/************/*****/index.php on line 61 Debug:Red colored text tell where (page and line) the output has started which is creating trouble. blue colored tells the calling php page and line Reason: Header can be sent once to the browser. If a header already sent you cant send another header later on the page. header get send when browser output something. Most commonly If header is already sent and you try to send another header using header() or try to set a cookie (cookies also sent with response headers) using setcookie() you will see this error. Causes:

  • Most commonly it happends when Your include files has white spaces or any outputs or any outputs or white spaces in the calling file itself.
  • When page is encoded as UTF-8 with Byte Order Mark (BOM)

Solution:

  • If it is problem with BOM you can change the file encoding to UTF-8 without BOM
  • Make sure include files/calling files does not have any outputs. you can omit the end php block of your include files at last line ?> php interpreter can assume on its own when the block ends.
  • It is usualy better to do Proccessing before you do any outputs to browser. but at last resort still in case it is not a option you can use php output buffering

Edited by birbal
  • Like 6
Link to comment
Share on other sites

  • 1 month later...

headers already sent error could found also on when we put session @ after outputting something right ?

Link to comment
Share on other sites

yes, as session id also sent as cookie and sent with header it will show same error.

Link to comment
Share on other sites

  • 3 months later...
  • 2 weeks later...

Not to be mean or anything, but we should definitely not show or even give the idea that using mysql_query is a good idea anymore. I know that you mention we should use PDO or mysqli, but why continue on to show them how to get errors using mysql_query. Someone should write about how to get errors and read errors using PDO as it is tends to be quiet unless you force it to tell you.

Link to comment
Share on other sites

Not to be mean or anything, but we should definitely not show or even give the idea that using mysql_query is a good idea anymore. I know that you mention we should use PDO or mysqli, but why continue on to show them how to get errors using mysql_query. Someone should write about how to get errors and read errors using PDO as it is tends to be quiet unless you force it to tell you.
This thread was made because of the amount of people that come to us asking about this particular error.
Link to comment
Share on other sites

This thread was made because of the amount of people that come to us asking about this particular error.
I acknowledge what you all are trying to do, and I am not trolling. I really just not a fan of correcting outdated deprecated code. You wouldn't want to fix something that you know will be on the next update or so. That makes me question why it is that we aren't fixing their bigger issue first. Let me try to explain with a pathetic analogy :Pleased: . If you had a problem with a small part of your car's muffler, and that part was recalled, wouldn't you want them to show you how to get a new muffler rather than to fix your old broken muffler? eh, I give up lol
Link to comment
Share on other sites

it always takes a while for what is deemed to be deprecated, actually become deprectated when servers, browsers, etc stop supporting it. but it takes many iterations of server software, browsers, etc for those features to get phased out. in the meantime, people will still be using these features, and as such, a reference for these features is still relevant. if in five years per se, people were still use mysql, then yeah, I'd say "yeah, get with the program", haha.

Link to comment
Share on other sites

  • 3 weeks later...

The battle against the mysql extension is ongoing. There's no reason to use it anymore for anything, but as long as the tutorials on w3schools.com use it as an example (which they still do), it's hard for us to ignore it here. I would like it if they removed all references to mysql in their tutorials and used mysqli for the basic tutorials, with additional tutorials using PDO. I tried to pick my words carefully in the initial post, like capitalizing MySQL to try and imply that it is any function that deals with MySQL instead of the mysql extension specifically, but, like old versions of IE, the mysql extension is hard to ignore at this forum. I would prefer to see people who reply to questions about the mysql extension respond with information about mysqli though.

Link to comment
Share on other sites

  • 3 months later...
  • 4 months later...

Hello everyone,

 

Just a quick clarification... isn't something like the following suppose to give the "headers already sent" error? I tried the following and didn't get an error.

<?phpsession_start();header('location: http://www.google.com');?>

I read sometime ago that having session_start() and then have header() sometime after can cause the error. Thanks.

Edited by Don E
Link to comment
Share on other sites

As long as there are no echo statements and there's nothing outside <?php ?> tags then you won't get the error.

 

This would give you a headers already sent error

<?phpsession_start();// The following three line breaks are considered output// Headers cannot be sent after output is sent to the client.?>  <?phpheader('location: http://www.google.com');?>
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...