This question already has an answer here:
<!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>
the same code was working in my desktop but in laptop it is showing mysql_fetch_array() expects parameter 1 to be resource, boolean given... error in while loop. what would be the reason? i just want to fetch renewal date from database and do some calculations. logic is completely fine since it was working perfectly in my desktop, but in laptop this error is occuring, please help. `
</div>
First :
Stop using mysql_query as it has been deprecated.
Second: Even though you are using it. PLease do not mix it with mysqli
Third: If you have used a placeholder in your query then please do not forget to bind the value as well
$sql = "SELECT Renewal_Date FROM ilist where Student_Id=?";
YOU forgot to bind the value for ?
You need to check the result of your query before you try to use it. If there's a mistake, the return value will be false (i.e. boolean rather than a MySQL resource). When that happens, you can find out what went wrong by calling mysqli_error()
. That should help you track down the problem.
mysql_fetch_array() expects parameter 1 to be resource, boolean given
This error occures because $mydata
value is actually false
.
mysqli_query
/mysql_query
functions returns resource if SQL query was executed successfully and false
value if there was an error.
How to deal with the problem:
mysql_
and mysqli_
);?
with escaped value in SQL statment $sql = "SELECT Renewal_Date FROM ilist where Student_Id=?";
or bind
value with Mysqli
extension.P.S.: Common advice is not to use mysql_
extension cause it is deprecated. Use PDO
or Mysqli
instead.