警告:mysql_num_rows():提供的参数无效

时间:2010-11-30 07:54:00

标签: php mysql

嗨我收到下面的错误,我已经仔细检查了一切,不知道为什么显示它。 警告:mysql_num_rows():提供的参数不是第6行/home/namebob/public_html/site_reg/statlookup.php中的有效MySQL结果资源

<?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)
    {
...

4 个答案:

答案 0 :(得分:1)

值得尝试var_dump($scan),如果你得到类似null / false的东西,那就意味着查询失败了。如果是这种情况,请尝试echo mysql_error()检查是否有任何sql错误(缺少表格等)

答案 1 :(得分:1)

很可能您的查询失败了。尝试:

if($scan) {
  if(mysql_num_rows($scan) > 0) {
    //...
  }
} else {
   trigger_error(mysql_error()); 
};

答案 2 :(得分: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;
        }   
    }       

答案 3 :(得分:1)

$scan = mysql_query("SELECT  `id`, `state` FROM `mast_state` WHERE `countryid` = '".mysql_real_escape_string($cid)."'");

但实际上,请使用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) ) {

  }
}