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

hi,
i am having a issue with my statement which is

function get_subjects($name){
        $query = mysql_query("SELECT * FROM subjects WHERE id = {$name} LIMIT 1");
                if($results = mysql_fetch_array($query)){
                    return $results;
                }else{
                return NULL;
                }
            }

i checked the statement the statement is phpmyadmin and its working fine. something happens when it fetches the array.any help?

Based on the php manual you have an error with your query. My guess is $name is a string and not an $id number. echo out the value of $name to verify that when it is inserted into the query, that the query is valid.

how do i echo a argument before its called? because i replace the $name with the id i got from $_GET super global.

function get_subjects($name){
        $sql = "SELECT * FROM subjects WHERE id = {$name} LIMIT 1";
        echo $sql; // comment out this line after you fix the mysql query issue
        $query = mysql_query($sql);
                if($results = mysql_fetch_array($query)){
                    return $results;
                }else{
                return NULL;
                }
            }

In addition to echoing out the query, when you are debugging you can use the ‘or die’ construction to get more info about the error if there is one:


function get_subjects($name) {
  $sql = "SELECT * FROM subjects WHERE id = {$name} LIMIT 1";
  echo $sql; // comment out this line after you fix the mysql query issue
  $query = mysql_query($sql) or die(' mysql error ' . mysql_error());
  if ($results = mysql_fetch_array($query)) {
    return $results;
  } else {
    return NULL;
  }
}

it says
mysql error Query was empty
i don’t understand. why is the query empty. whereas i ran it in phpmyadmin and it worked fine

Please repost the code you’re using now (with all the changes made after our comments) and the value of $sql.

function get_subjects($name){
        $mysql = mysql_query("SELECT * FROM subjects WHERE id = {$name} LIMIT 1");
        echo $mysql;
        $query = mysql_query($mysql) or die(' mysql error ' . mysql_error());
                if($results = mysql_fetch_array($query)){
                    return $results;
                }else{
                return NULL;
                }
            }

Output comes saying mysql error Query was empty

i saw one my mistake when i put two times mysql_query. now its saying no db selected. where as i pulled the global $con into the function

You put the global $con into the function.

Show us your connection line, where you defined $con. (Obscure your password.)


$con = mysql_connect("localhost","root","");

The problem in my view i m watching lynda.com php and mysql essential training and when kevin skougland do refactoring, everything get messed up. i think refactoring is a very advance level for me. still i m trying my way around it. just stuck on this step.

So the problem is quite clearly stated to you.

“no db selected”

As you have not displayed a [FPHP]mysql_select_db[/FPHP] command in your paste, i’m assuming you dont have one. Specify one and you should clear that error :wink:

I will try that, i have another question

$page_set =mysql_query("SELECT * FROM pages WHERE subject_id= {subjects['id']}");

is there a way that i can see the output of the query because its not working. like generated code.

You missed the $ in front of subjects.

Thank you so much guys. it helped. please advice me for practicing PHP and being more strong in logic

Maybe you could try to apply what is being explained to you? If you used the tips given in this thread to this new query (put the query string in a variable, echo out that variable, use the ‘or die’ construction) then you probably would have found that error yourself.

yes i did, and it was helpful. i want to know what is the best way to practice when you don’t have projects to work on and be strong in php/mysql

Best practice would be to use the or die(mysql_error()) implementation instead of echo $sql; (my opinion).

Another thing you should do (if developing locally), is the first item in this thread

I have a question like a function argument. like

we are passing a argument and when we want to echo it to see what is query. what would the passed in argument will return coz we haven’t called that function

Can you post an example of your code so I can verify that I understand your question?