SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    524
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Really stupid debugging question because I'm losing my mind

    I'm trying to debug some code from previous developer (ie I wouldn't normally program like this) and I've got an error I just can't get rid of. The code below results in the output beneath it.

    What I can't figure out is how execution gets to the point of error, because the query result has already been checked (and output). Basically I'm confused and am losing the plot. Can anyone help?

    PHP Code:
    $res   mysql_query($query);
    if(!
    $res) continue; //skip out of this loop iteration

    $rows mysql_num_rows($res);

    if (
    $rows>0)
    {
        echo 
    "Res : ";
        
    var_dump($res);
        while (
    $row mysql_fetch_array($res)) //this is line 125
        
    {
    ...... 
    Code:
    Res : resource(22) of type (mysql result)
    PHP Warning:  mysql_fetch_array() expects parameter 1 to be resource, boolean given in <snip />/getDetails.php on line 125
    Last edited by DaveMaxwell; Mar 21, 2013 at 07:49. Reason: removed URL for security concerns....

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,819
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Try this

    PHP Code:

    While ($res && $row= ... 

  3. #3
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    524
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    Try this

    PHP Code:

    While ($res && $row= ... 
    Good suggestion. Still goes through and errors:
    PHP Code:
    echo "\nalso checking res in while condition : \n";
    while (
    $res && $row mysql_fetch_array($res)){ 
    Code:
    also checking res in while condition : 
    PHP Warning:  mysql_fetch_array() expects parameter 1 to be resource, boolean given in myscript.php on line 126

  4. #4
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    989
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    you say it's in the loop. My guess is that the first loop is executing successfully but the second (or last) query is returning an invalid result.

    I suspect $res is being left over from a previous iteration.
    Try this:

    PHP Code:

    $res   
    mysql_query($query); 
    if(!
    $res) {
    echo 
    mysql_error();
    continue; 
    //skip out of this loop iteration 


  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,819
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by hessodreamy View Post
    Good suggestion. Still goes through and errors:
    PHP Code:
    echo "\nalso checking res in while condition : \n";
    while (
    $res && $row mysql_fetch_array($res)){ 
    Code:
    also checking res in while condition : 
    PHP Warning:  mysql_fetch_array() expects parameter 1 to be resource, boolean given in myscript.php on line 126
    OK try again

    PHP Code:

    while ( is_resource($res) && $row mysql_fetch_array($res))

    ...
    ...
    ...


  6. #6
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    524
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    OK try again

    PHP Code:

    while ( is_resource($res) && $row mysql_fetch_array($res))

    ...
    ...
    ...

    Well yeah, that seems to have done the trick. In that it's not giving an error because it doesn't try to execute the mysql_fetch_array. Even though it probably should.

    I think Tom is probably right in that variables are being overwritten by previous iterations and other looping structures.

    I think this code is too far gone to be able to live. I was hoping to understand and fix this bug but I think I'm going to rip it all out and start again.

  7. #7
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Have you tried installing xdebug and using breakpoints to walk through the code a line at a time to see what's going on?

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (!$res
    If the result of the MySQL query fails, why would you want to continue and try to extract results? (From a query result that doesn't exist). Or am I reading things wrong?
    Ian Anderson
    www.siteguru.co.uk

  9. #9
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    989
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by siteguru View Post
    PHP Code:
    if (!$res
    If the result of the MySQL query fails, why would you want to continue and try to extract results? (From a query result that doesn't exist). Or am I reading things wrong?
    You're reading it wrong, the continue keyword skips the rest of the current iteration and moves on to the next one.


    e.g.


    PHP Code:

    for ($i 0$i 5$i++ ) {

          if (
    $i == 2) continue;

          echo 
    $i "\n";


    Prints:

    Code:
    0
    1
    3
    4

  10. #10
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I can understand that example. But in the OP's code there IS no iteration as such ... it is a single expression that is not within a loop.
    Ian Anderson
    www.siteguru.co.uk

  11. #11
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    989
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    I'm assuming that hessodreamy had posted a snippet and simply implied the loops existence. It clearly isn't the full set of code. Though, correct me if I'm wrong, @hessodreamy !


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
  •