SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 29

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

  1. #1
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    hi,
    i am having a issue with my statement which is
    Code PHP:
    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?
    All those who wander aren't lost.

  2. #2
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,944
    Mentioned
    49 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do i echo a argument before its called? because i replace the $name with the id i got from $_GET super global.
    All those who wander aren't lost.

  4. #4
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,944
    Mentioned
    49 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    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;
                    }
                }

  5. #5
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,642
    Mentioned
    77 Post(s)
    Tagged
    4 Thread(s)
    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:
    PHP Code:
    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;
      }


  6. #6
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    PHP Code:
    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
    All those who wander aren't lost.

  7. #7
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,642
    Mentioned
    77 Post(s)
    Tagged
    4 Thread(s)
    Please repost the code you're using now (with all the changes made after our comments) and the value of $sql.

  8. #8
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Please repost the code you're using now (with all the changes made after our comments) and the value of $sql.
    Code PHP:
    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
    All those who wander aren't lost.

  9. #9
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    All those who wander aren't lost.

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,560
    Mentioned
    35 Post(s)
    Tagged
    0 Thread(s)
    You put the global $con into the function.

    Show us your connection line, where you defined $con. (Obscure your password.)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  11. #11
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    $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.
    All those who wander aren't lost.

  12. #12
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,560
    Mentioned
    35 Post(s)
    Tagged
    0 Thread(s)
    So the problem is quite clearly stated to you.

    "no db selected"

    As you have not displayed a mysql_select_db command in your paste, i'm assuming you dont have one. Specify one and you should clear that error
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  13. #13
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will try that, i have another question
    Code PHP:
    $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.
    All those who wander aren't lost.

  14. #14
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,560
    Mentioned
    35 Post(s)
    Tagged
    0 Thread(s)
    You missed the $ in front of subjects.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  15. #15
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much guys. it helped. please advice me for practicing PHP and being more strong in logic
    All those who wander aren't lost.

  16. #16
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,642
    Mentioned
    77 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by nofel View Post
    I will try that, i have another question
    Code PHP:
    $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.
    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.

  17. #17
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    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
    All those who wander aren't lost.

  18. #18
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,944
    Mentioned
    49 Post(s)
    Tagged
    0 Thread(s)
    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

  19. #19
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    All those who wander aren't lost.

  20. #20
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,944
    Mentioned
    49 Post(s)
    Tagged
    0 Thread(s)
    Can you post an example of your code so I can verify that I understand your question?

  21. #21
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    function get_subjects($name){
            mysql_select_db(DB_NAME);
            $mysql = ("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;
                    }
                }
    like this example. when we echo it to ask what is the generated query. how would it know what to give in place for $name as its a args.
    All those who wander aren't lost.

  22. #22
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,642
    Mentioned
    77 Post(s)
    Tagged
    4 Thread(s)
    It will use the parameter you've passed to the function call.

  23. #23
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    It will use the parameter you've passed to the function call.
    like what? output the {$name} as it is?
    All those who wander aren't lost.

  24. #24
    Goofy nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also i had run into some trouble of i think default values when i load the page. i created a new thread. i would be glad if u guide me.
    All those who wander aren't lost.

  25. #25
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,642
    Mentioned
    77 Post(s)
    Tagged
    4 Thread(s)
    Code:
    function get_subjects($name) {
            mysql_select_db(DB_NAME);
            $mysql = "SELECT * FROM subjects WHERE id = {$name} LIMIT 1";
            echo $mysql;
            ....
    }
    
    // example call 1
    $subjects = get_subjects('subjectname');
    
    // example call 2
    $subjects = get_subjects('100');
    
    // example call 3
    $subjects = get_subjects('');
    Example call 1 wil display the following:
    Code:
    SELECT * FROM subjects WHERE id = {subjectname} LIMIT 1
    Example call 2 wil display the following:
    Code:
    SELECT * FROM subjects WHERE id = {100} LIMIT 1
    Example call 1 wil display the following:
    Code:
    SELECT * FROM subjects WHERE id = LIMIT 1
    It "knows" what value of $name to use, because you pass that name when you call the get_subjects function.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •