0

Hi i'm receiving the error below, i've double checked everything and don't know why its being displayed. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/namebob/public_html/site_reg/statlookup.php on line 6

<?php
    include_once('common.php');
    $cid        =   $_REQUEST['cid'];
    $_elmID     =   $_REQUEST['elmID'];
    $scan = mysql_query("SELECT  `id`, `state` FROM `mast_state` WHERE `countryid` = $cid");
    if(mysql_num_rows($scan)>0)
    {
...
acctman
  • 4,229
  • 30
  • 98
  • 142
  • 1
    `$cid = $_REQUEST['cid'];` should be `$cid = (int) $_REQUEST['cid'];` in order to avoid SQL injection attacks. – tamasd Nov 30 '10 at 08:14
  • @Yorirou, thats only if cid is a number. He should be quoting it, or using the autoquoting capabilities of PDO. – Rahly Nov 30 '10 at 08:19
  • As a matter of fact, you didn't "double-check" anything.Just because you don't handle any errors. Error handling (and debugging) are most important parts of programming, but not a single newbie ever heard of that. Actually you don't need to double-check anything. Make your program tell you what is going wrong - it would be way more reliable. See my other answer how to properly run mysql queries: http://stackoverflow.com/questions/2666104/how-to-reslove-mysql-fetch-assoc-problems/2666119#2666119 – Your Common Sense Nov 30 '10 at 08:50
  • 1
    possible duplicate of [mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in php](http://stackoverflow.com/questions/1858304/mysql-fetch-assoc-supplied-argument-is-not-a-valid-mysql-result-resource-in-ph) – Lightness Races in Orbit Sep 16 '11 at 17:51
  • possible duplicate of [Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error](http://stackoverflow.com/questions/11674312/warning-mysql-fetch-expects-parameter-1-to-be-resource-boolean-given-error) – DCoder Aug 09 '12 at 19:30
  • possible duplicate of [Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result](http://stackoverflow.com/questions/795746/warning-mysql-fetch-array-supplied-argument-is-not-a-valid-mysql-result) – Fluffeh Aug 10 '12 at 09:05

4 Answers4

1

It's worth trying var_dump($scan), if you get something like null / false, that means the query fails. If that's the case, try echo mysql_error() to check for any sql error (missing table etc)

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
  • i'm getting a false turn for the dump, and for the last error i get this Fatal error: Call to undefined function mysql_last_error() in – acctman Nov 30 '10 at 08:09
  • do not use echo but rather trigger_error() to display errors. it shouldn't go on the screen on the production server – Your Common Sense Nov 30 '10 at 08:31
1

Most probably your query is failing. Try:

if($scan) {
  if(mysql_num_rows($scan) > 0) {
    //...
  }
} else {
   trigger_error(mysql_error()); 
};
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • this is the error i'm receiving 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 '' at line 1 – acctman Nov 30 '10 at 08:10
  • then the sql statement you are running is invalid and also has sql injection ability. use PDO ;-) – Rahly Nov 30 '10 at 08:12
  • @Jeremy most likely it's because you don't have elmID in the request. Add `error_reporting(E_ALL);` at the top of this code to be informed of such silly mistakes – Your Common Sense Nov 30 '10 at 08:29
1
$scan = mysql_query("SELECT  `id`, `state` FROM `mast_state` WHERE `countryid` = $cid");
    $stock_num = '';
    if($scan )
    {
        while($row=mysql_fetch_object($scan))
        {
            if($row->state)
            $state  = $row->state;
        }   
    }       
Pops
  • 30,199
  • 37
  • 136
  • 151
Pradeep Singh
  • 3,582
  • 3
  • 29
  • 42
1
$scan = mysql_query("SELECT  `id`, `state` FROM `mast_state` WHERE `countryid` = '".mysql_real_escape_string($cid)."'");

But really, use PDO.

$stm = $db->prepare("SELECT  `id`, `state` FROM `mast_state` WHERE `countryid` = ?");
if( $stm && $stm->execute(array($cid)) ) {
  while( $data = $stm->fetch(PDO::FETCH_ASSOC) ) {

  }
}
Rahly
  • 1,462
  • 13
  • 16
  • he wanted to know the number of found rows, didn't he? – Your Common Sense Nov 30 '10 at 08:26
  • it doesn't look like it as hes checking to see if the row count is greater than 0. That's a general indication that he is just looking to see if the query returned anything back to him. He could just $stm->rowCount to get the row count. – Rahly Nov 30 '10 at 16:31