mysql error

Discussion in 'MySQL' started by furious, Dec 13, 2009.

  1. #1
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /web/sites/user/www.someurl.com/stpexport/index.php on line 10

    <?php
    include('./mysql.php');
    $no=$_GET["no"];
    $sort=$_GET["sort"];
    
    $sql = "SELECT * FROM `stp_videos` WHERE `convert_status`= 'done' ORDER BY `stp_videos`.`id` $sort LIMIT 0,$no";
    
    $result = mysql_query($sql);
    
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    
    
    $mins = floor ($row["runtime"] / 60);
    $secs = $row["runtime"] % 60;
    
    
        echo "http://www.someurl.com/index.php?view=video&v=".$row["cache_id"]."|http://img.someurl.com/videos/".$row["subdir"]."/".$row["id"]."/5.jpg|".$row["title"]."|".$mins."|".$secs."<br/>\n";
    } 
    
    
    mysql_close($conn);
    
    ?>
    Code (markup):
    any ideas whats the problem?

    thanks!
     
    furious, Dec 13, 2009 IP
  2. kingsoflegend

    kingsoflegend Well-Known Member

    Messages:
    202
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You either made a typo in one of the column names or the "no" and "sort" variables are breaking the query.

    PS. google "sql injection attacks" and learn something.
     
    kingsoflegend, Dec 13, 2009 IP
  3. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #3
    There seems to be a problem with your query. Print $sql, it will show the resultant query.
     
    mwasif, Dec 13, 2009 IP
  4. alexpr07

    alexpr07 Active Member

    Messages:
    284
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #4
    Well, I see this: "LIMIT 0"
    How it supposed to show any records where you limited it to ZERO records? Perhaps this is a mistake.

    or

    Well, I'm not good with PHP but you put 2 variables in the query $sort and $no
    Don't you have to use this syntax when working with strings: "string1"."string2"
    like in your example below:

    $sql = "SELECT * FROM `stp_videos` WHERE `convert_status`= 'done' ORDER BY `stp_videos`.`id` ".$sort."LIMIT 0,".$no;


    Let me know if I'm right cause I'm curious about it.
     
    alexpr07, Dec 13, 2009 IP
  5. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Print query before execution, if you can not identify a problem visually, pick up query and execute it manually using phpMyAdmin of whatever tool you are using. You will get error message if there is a problem with query.

    If you are unable to figure out solution for error in query, do let us know the resultant query for further help :)
     
    mastermunj, Dec 14, 2009 IP
  6. kingsoflegend

    kingsoflegend Well-Known Member

    Messages:
    202
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #6
    Even if that was the problem, it still wouldn't cause an SQL error, it would just return an empty result.

    Since you obviously don't know basic SQL, I'll give you a quick lesson. The LIMIT attribute can be used in 2 ways:
    1. LIMIT 10 which means that the query will return only 10 results
    2. LIMIT 10, 20 which means that the query will return a maximum of 20 results while skipping the first 10. In other words, the first number is the offset and the second is the actual limit.

    In this case LIMIT 0, $no will display the $no results starting with the first. This is equivalent with LIMIT $no.
     
    kingsoflegend, Dec 16, 2009 IP
  7. alexpr07

    alexpr07 Active Member

    Messages:
    284
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #7
    Thanks for the lesson in MySQL. I'm self-taught :)

    Anyway, this rules out my first theory, but what about a second?

    $sql="....LIMIT 0, $no"; or it's supposed to be $sql="...LIMIT 0, ".$no;

    as $no is a variable and not part of the query text.
     
    alexpr07, Dec 22, 2009 IP
  8. kingsoflegend

    kingsoflegend Well-Known Member

    Messages:
    202
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #8
    It doesn't look like you taught yourself anything. Right now you're at a level where you'd struggle with a hello world program. Please go away and don't post anything until you actually learn something useful.
     
    kingsoflegend, Dec 22, 2009 IP
  9. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #9
    Try to understand what others are saying. If $no is empty (I am assuming ASC for $sort), you'll get the following resultant query
    SELECT * FROM `stp_videos` WHERE `convert_status`= 'done' ORDER BY `stp_videos`.`id` ASC LIMIT 0,
    Code (markup):
    If $no has some value lets say 10 then the query will be
    SELECT * FROM `stp_videos` WHERE `convert_status`= 'done' ORDER BY `stp_videos`.`id` ASC LIMIT 0,10
    Code (markup):
    Now execute both the queries in MySQL and you'll see the difference which one the people are trying to say.
     
    mwasif, Dec 22, 2009 IP