This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result
                                
                                    (32个答案)
                                
                        
                                6年前关闭。
            
                    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
 $con = mysql_connect("localhost","root","");
    if(!$con)
    {
    die("cannot connect" . mysql_error());
    }
    mysql_select_db("library",$con);

    $sql = "SELECT Renewal_Date FROM ilist where Student_Id=?";
    $mydata = mysqli_query($sql,$con);
    $result_array = array();
     echo "<table border=2 >
    <tr>

       <th>Renewal_Date</th>
    </tr>";


  while($record = mysql_fetch_array($mydata)){

  echo"<tr>";
  echo "<td>" . $record['Renewal_Date'] . "</td>";
  echo "</tr>";
  $todayDate = date('m/d/Y');
  $date1 = new DateTime($todayDate);
  $date2 = new DateTime($record['Renewal_Date']);
  if($date2 < $date1)
    {
  $interval = $date1->diff($date2);
  echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d."        days ";
  echo"<p>";
  $extradays=$interval->m;
if($extradays==1)
{
    $eday=1;
    }else if($extradays==3){
        $eday=2;
        }   else if($extradays==5){
            $eday=3;
                }   else if($extradays==7){
                    $eday=4;
                    }   else if($extradays==8){
                        $eday=5;
                        }   else if($extradays==10){
                            $eday=6;
                            }   else if($extradays==12){
                                $eday=7;
                                }
                                else{
            $eday=0;
        }

$yd= $interval->y*365*3+$interval->d*3;

    $ydm=$yd+$interval->m*30*3;
    $ydm=$ydm+($eday*3);
    echo $ydm;
}
else
{
    echo $ydm=0;
    }
}
?>
<body>
</body>
</html>


相同的代码在我的台式机上工作,但在笔记本电脑上,它显示mysql_fetch_array()期望参数1为资源,给定布尔值... while循环中出错。这是什么原因?我只想从数据库中获取续订日期并进行一些计算。逻辑完全可以正常运行,因为它在我的台式机上运行正常,但是在笔记本电脑中会出现此错误,请提供帮助。
`

最佳答案

首先:
不再使用mysql_query,因为它已被弃用。
第二:
即使您正在使用它。请不要将其与mysqli混合使用
第三:
如果您在查询中使用了占位符,那么也请不要忘记绑定该值

$sql = "SELECT Renewal_Date FROM ilist where Student_Id=?";


您忘记绑定值了吗?

08-06 21:00