Warning: mysql_num_rows()

Discussion in 'PHP' started by CuBz, Aug 14, 2010.

  1. #1
    I am getting this error

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/myuser/public_html/saml/oc.php on line 35
    Code (markup):
    Line 35 is shown below
    $username=$_SESSION['username'];
    $above = mysql_query("SELECT * FROM users WHERE username='$username'");
    $fetch = mysql_fetch_object($above);
    
    
    $oc_id=strip_tags($_GET['oc_id']);
    if (strip_tags($_GET['place']) == "we"){
    $use="we_inv";
    $a="we";
    $query= "SELECT * FROM oc WHERE we_inv='$username' AND id='$oc_id'";
    }elseif (strip_tags($_GET['place']) == "ee"){
    $use="ee_inv";
    $a="ee";
    $query= "SELECT * FROM oc WHERE ee_inv='$username' AND id='$oc_id'";
    
    }elseif (strip_tags($_GET['place']) == "driver"){
    $use="driver_inv";
    $a="driver";
    $query= "SELECT * FROM oc WHERE driver_inv='$username' AND id='$oc_id'";
    }
    
    $round=mysql_query($query);
    $check=mysql_num_rows($round); <----LINE 35 HERE
    if ($check != "0"){
    
    mysql_query("UPDATE `oc` SET `$a`='$username' WHERE `id`='$oc_id'");
    mysql_query("UPDATE `users` SET `oc`='1' WHERE `username`='$username'");
    echo "<SCRIPT LANGUAGE='JavaScript'>
    window.location='oc.php';
    
    </script>";
    }
    
    PHP:

     
    CuBz, Aug 14, 2010 IP
  2. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #2
    There may be an error in the query thats why $round isn't a valid resource try editing line 34 to:
    $round=mysql_query($query) or die(mysql_error());
    PHP:
    to see if there is an error.

    Cheers!
     
    Narrator, Aug 14, 2010 IP
  3. CuBz

    CuBz Peon

    Messages:
    117
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes thanks.

    I got the following error

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1
    Code (markup):
     
    CuBz, Aug 14, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    It's likely caused by one of your user submitted variables not being set/defined, theirfore I've added a few if statements to check - also improved your code slightly (to make it more readable)...use the following code, and let me know how that goes (also quote any errors - if any; within your next post):

    <?php
    error_reporting(E_ALL);
    if (!isset($_SESSON['username'])) {
        echo 'You must be logged in!';
    } elseif (!isset($_GET['oc_id'])) {
        echo 'oc_id is not set!';
    } elseif (!isset($_GET['place'])) {
        echo 'place is not set!';
    } else {
        $username = $_SESSION['username'];
        $above    = mysql_query("SELECT * FROM users WHERE username = '{$username}'");
        $fetch    = mysql_fetch_object($above);
    
        $oc_id = intval($_GET['oc_id']);
        if ($_GET['place'] == 'we') {
            $use   = 'we_inv';
            $a     = 'we';
            $query = "SELECT * FROM oc WHERE we_inv = '{$username}' AND id = '{$oc_id}'";
        } elseif ($_GET['place'] == 'ee') {
            $use   = 'ee_inv';
            $a     = 'ee';
            $query = "SELECT * FROM oc WHERE ee_inv = '{$username}' AND id = '{$oc_id}'";
            
        } elseif ($_GET['place'] == 'driver') {
            $use   = 'driver_inv';
            $a     = 'driver';
            $query = "SELECT * FROM oc WHERE driver_inv = '{$username}' AND id = '{$oc_id}'";
        }
        
        $round = mysql_query($query);
        $check = mysql_num_rows($round); 
        if ($check != 0):
            mysql_query("UPDATE oc SET {$a} = '{$username}' WHERE id = '{$oc_id}'");
            mysql_query("UPDATE users SET oc = '1' WHERE username = '{$username}'");
    ?>
    <script language='JavaScript'>
    window.location='oc.php';
    </script>
    <?php
        endif;
    }
    ?>
    PHP:
     
    danx10, Aug 14, 2010 IP
  5. CuBz

    CuBz Peon

    Messages:
    117
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That didn't work but I noticed that I used $query twice, so i changed it to $getit and now get the error below

    Query was empty
    Code (markup):
     
    CuBz, Aug 14, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Erm that isn't an error? (or atleast from my entire time doing PHP havn't ever come accross that :/)

    Your current code sets a variable $query based on the $_GET...and then later on in the script you use that $query.

    So if those $_GET's are not set you'll get an error as their is no query to execute.

    The same applies for the $_SESSION['username']

    Theirfore:

    - make sure all variables exist/are set so it can query properly.

    - make sure all column names and table names are exist!
     
    Last edited: Aug 14, 2010
    danx10, Aug 14, 2010 IP