SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question mysql_fetch_array() problem

    I have this code as part of a function but for any reason the while loop is not running. This is the code:
    PHP Code:
    $get "SELECT * FROM ".TBL." WHERE test = '1' && (test = '".$fieldx['ShortVersion']."' || test2 = '".$fieldx['ShortVersion']."' || test3 = '".$fieldx['ShortVersion']."' || test4 = '".$fieldx['ShortVersion']."')";  
                    
    $getprocess mysql_query($get) or die(mysql_error()); 
                    echo 
    "<br>Awarding ..."
                    
    //Debug ready till here 
                    
    while($getup mysql_fetch_array($getprocess)) { 
                        
    // This does not run 
                    
    }; 
    When I add or die("error".mysql_error()) to the mysql_fetch_array(), it shows "error" but nothing related to mysql. Can someone help please?

  2. #2
    SitePoint Addict bronze trophy Hall of Famer's Avatar
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    371
    Mentioned
    6 Post(s)
    Tagged
    2 Thread(s)
    Well try not to use MySQL extensions such as mysql_query and mysql_fetch_array as they are deprecated. Instead, use mysqli or PDO classes/objects will significantly improve your script.

    For your problem, it seems that the query did not return any results. Either there is no record with the criterion you specify, or that there is a syntax error in SQL string.

  3. #3
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    Is TBL realy your table name?

    I would write FROM ".TBL." WHERE as FROM TBL WHERE

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,889
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    What is the result of echo $get; and echo mysql_error();
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  5. #5
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I fixed it by doing this:
    Code:
    $get = "SELECT * FROM ".TBL." WHERE test = '1'
            AND '{$fieldx['ShortVersion']}' IN (test1, test2, test3, test4)";
    . I researched a little to find it out but it's running fine.

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    MySQL doesn't support ||, use OR instead.
    Though I do like your second variant with IN better because it's shorter and more readable.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aah, thanks to you I found the reason why the first version was not working. Yeah, the new one is much better.

  8. #8
    SitePoint Member
    Join Date
    Oct 2013
    Location
    Colorado
    Posts
    20
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Liongold View Post
    I have this code as part of a function but for any reason the while loop is not running. This is the code:
    PHP Code:
    $get "SELECT * FROM ".TBL." WHERE test = '1' && (test = '".$fieldx['ShortVersion']."' || test2 = '".$fieldx['ShortVersion']."' || test3 = '".$fieldx['ShortVersion']."' || test4 = '".$fieldx['ShortVersion']."')";  
                    
    $getprocess mysql_query($get) or die(mysql_error()); 
                    echo 
    "<br>Awarding ..."
                    
    //Debug ready till here 
                    
    while($getup mysql_fetch_array($getprocess)) { 
                        
    // This does not run 
                    
    }; 
    When I add or die("error".mysql_error()) to the mysql_fetch_array(), it shows "error" but nothing related to mysql. Can someone help please?

    Where you concatenate the table name into your query ( $get = "SELECT * FROM ". TBL . "WHERE...) it does not have a $ symbol in front of the TBL variable. Maybe that is causing a problem.

  9. #9
    SitePoint Member
    Join Date
    Oct 2013
    Location
    Colorado
    Posts
    20
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, TBL must be a constant so my suggestion is irrelevant...


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
  •