Fetch array error

Hi,
i wanted to know why this error comes

mysql_fetch_array() expects parameter 1 to be resource, string given

een faced with error more than any other.

Because mysql_fetch_array() expects parameter 1 to be a mysql result set, and for some reason it isn’t. Most probably because the query has ended with an error. But that’s impossible to tell without seeing some code.

Well, generally i seen that error a lot. what’s the right way debug it? also i have pasted the code below too

    function get_pages($subject_id){
                $result = mysql_query("SELECT * FROM pages where subject_id = {$subject_id}");
                return($subject_id);
    }
$result = get_pages($subject["id"]);
                while($row = mysql_fetch_array($result)){
                echo "<li>".$row["menu"]."</li>";
                }

What are you returning from that function?

:slight_smile: thanks for pointing out. i was returning the id, which wasn’t being returned there. :stuck_out_tongue: i fixed it and it worked. although my question remain, how would i Debug it. like suppose i am unable to find a error

When you’re debugging, you could write your queries like this:


$query = "SELECT * FROM pages where subject_id = {$subject_id}";
$result = mysql_query($query) or die('mysql error ' . mysql_error() . ' in query ' . $query);

That will show you the mysql error and the query that causes it when the query ends with an error.

For other errors you might want to add some echo statements to see what parts of the code are being executed, and echo/print_r/var_dump statements to check the value of certain variables.

i m stuck with this problem. i get Resource id # 7. i donno wht is this error here is the code


function get_subjects_by_id($subject_id){
               $result = mysql_query("SELECT * FROM subjects WHERE id = {$subject_id} LIMIT 1");
               echo "$result"; exit;
               confirm($result);
               if($subject = mysql_fetch_array($result)){
                       return ($subject);
               }else{
                       return NULL;
               }

$sub = get_subjects_by_id($sel_sub);

the $sel_sb is passing id, and it works fine when i echo $sel_sub.

what does confirm() do?

              echo "$result"; exit;

That’s not an error. It’s the result of this line of code. $result contains a mysql result set (if the query has finished ok), and echoing it shows 'Resource id # …"

But the $result should produce a array containing the elements in that column, why resource,
StarLion confirm has die command, if anything doesnt work

Well, somewhere between $result = mysql_query(“SELECT * FROM subjects WHERE id = {$subject_id} LIMIT 1”);
and
if($subject = mysql_fetch_array($result)){
, $result is turning into a string, according to your error message…

I would look at something like


function get_subjects_by_id($subject_id){


	$sql = "SELECT * FROM subjects WHERE id = {$subject_id} LIMIT 1";


	$result = mysql_query($sql) or die(mysql_error());
	
	confirm($result);
	
	if($subject = mysql_fetch_array($result)) {
       	        return ($subject);
	} else {
		echo $sql;	
	}  
}

Get rid of this line:

echo "$result"; exit;  

$result is not an array, it’s the result set from the query.
$subject should contain the array and is returned from the function.

ok i simplified it more as spikeZ said and again i m getting error saying

mysql_fetch_array() expects parameter 1 to be resource, boolean given

here is my query

$query = mysql_query("SELECT * FROM subjects WHERE id ={$subject["id"]}");
        if($subject = mysql_fetch_array($query)) {
                           return ($subject);
            } else {
                echo $query;    
            } 

i added the die and mydql_error() parameter, it says

You have an error in your SQL syntax; check the manual that corresponds  to your MySQL server version for the right syntax to use near ''

wonder what i m doing wrong

The problem right now is you’re using double quotes inside double quotes. So you didn’t simplify as SpikeZ said, because in his code that error isn’t there.

ok, now i copied spikeZ code and i get this error

You have an error in your SQL syntax; check the manual that corresponds  to your MySQL server version for the right syntax to use near 'LIMIT 1'  at line 1

OK so lets simply it even further

function get_subjects_by_id($subject_id){
 
 
	$sql = "SELECT * FROM subjects WHERE id = $subject_id LIMIT 1";
 
 
	$result = mysql_query($sql) or die(mysql_error() . 'error='.  $sql);
 
	confirm($result);
 
	if($subject = mysql_fetch_array($result)) {
       	        return ($subject);
	} else {
		echo $sql;	
	}  
}

So now it will echo out the query along with the error message. Post the results please

here you go

You have an error in your SQL syntax; check the manual that corresponds  to your MySQL server version for the right syntax to use near 'LIMIT 1'  at line 1error=SELECT * FROM subjects WHERE id =  LIMIT 1

i also after this error, hard code subject_id, which returned the value from database, does it mean my function argument in the query isn’t working?

Yep pretty much. The $subject_id that the function needs is missing.
How is it being passed to it?

after couple of hours and banging my head against wall, i finally fixed the issue. had to take a break, rewrite the function and walla!, thanks a lot though