SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    fetch array error

    Hi,
    i wanted to know why this error comes
    PHP Code:
    mysql_fetch_array() expects parameter 1 to be resourcestring given 
    een faced with error more than any other.
    All those who wander aren't lost.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    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.

  3. #3
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, generally i seen that error a lot. what's the right way debug it? also i have pasted the code below too
    PHP Code:
        function get_pages($subject_id){
                    
    $result mysql_query("SELECT * FROM pages where subject_id = {$subject_id}");
                    return(
    $subject_id);
        } 
    PHP Code:
    $result get_pages($subject["id"]);
                    while(
    $row mysql_fetch_array($result)){
                    echo 
    "<li>".$row["menu"]."</li>";
                    } 
    All those who wander aren't lost.

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    What are you returning from that function?

  5. #5
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    What are you returning from that function?
    thanks for pointing out. i was returning the id, which wasn't being returned there. :P i fixed it and it worked. although my question remain, how would i Debug it. like suppose i am unable to find a error
    All those who wander aren't lost.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    When you're debugging, you could write your queries like this:
    PHP Code:
    $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.

  7. #7
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Resource id # 7

    i m stuck with this problem. i get Resource id # 7. i donno wht is this error here is the code
    PHP 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;
                   } 
    PHP Code:
    $sub get_subjects_by_id($sel_sub); 
    the $sel_sb is passing id, and it works fine when i echo $sel_sub.
    All those who wander aren't lost.

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    what does confirm() do?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  9. #9
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    PHP Code:
                  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 # ..."

  10. #10
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    But the $result should produce a array containing the elements in that column, why resource,
    StarLion confirm has die command, if anything doesnt work
    All those who wander aren't lost.

  11. #11
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    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...
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  12. #12
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    153 Post(s)
    Tagged
    3 Thread(s)
    I would look at something like

    Code PHP:
    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;	
    	}  
    }
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  13. #13
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Get rid of this line:
    PHP Code:
    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.

  14. #14
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ok i simplified it more as spikeZ said and again i m getting error saying
    PHP Code:
    mysql_fetch_array() expects parameter 1 to be resourceboolean given 
    here is my query
    PHP Code:
    $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
    PHP Code:
    You have an error in your SQL syntaxcheck the manual that corresponds  to your MySQL server version for the right syntax to use near '' 
    wonder what i m doing wrong
    All those who wander aren't lost.

  15. #15
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,412
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by nofel View Post
    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.

  16. #16
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ok, now i copied spikeZ code and i get this error
    PHP Code:
    You have an error in your SQL syntaxcheck the manual that corresponds  to your MySQL server version for the right syntax to use near 'LIMIT 1'  at line 1 
    All those who wander aren't lost.

  17. #17
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    153 Post(s)
    Tagged
    3 Thread(s)
    OK so lets simply it even further
    Code PHP:
    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
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  18. #18
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    here you go
    PHP Code:
    You have an error in your SQL syntaxcheck 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?
    All those who wander aren't lost.

  19. #19
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    153 Post(s)
    Tagged
    3 Thread(s)
    Yep pretty much. The $subject_id that the function needs is missing.
    How is it being passed to it?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

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


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
  •