This question already has an answer here:
hello guys i am new here and i am having a hard time in analyzing this code. an error keeps on showing and i dont know what to do.
mysql_fetch_array() expects parameter 1 to be resource, boolean given
this happens everytime i use the search. any ideas?
if(isset($_POST['search']))//if search
{
if(($_POST['year']) && ($_POST['month']) && ($_POST['day']))
$_SESSION['select']= mysql_query("SELECT * FROM ".$_SESSION['dbtable3']." WHERE eyear = '".$_POST['year']."' AND emonth = '".$_POST['month']."' AND edate = '".$_POST['day']."' ;" or die(mysql_error()));
else if(($_POST['year']) && ($_POST['month']))
$_SESSION['select']=mysql_query("SELECT * FROM ".$_SESSION['dbtable3']." WHERE eyear = '".$_POST['year']."' AND emonth = '".$_POST['month']."' ;" or die(mysql_error()));
else if(isset($_POST['year']))
$_SESSION['select']= mysql_query("SELECT * FROM ".$_SESSION['dbtable3']." WHERE eyear = '".$_POST['year']."' ;" or die(mysql_error()));
else if(!$_POST['year'])
die("FILL IN AT LEAST THE YEAR");
else
die("Date not found");
if($_POST['year'])
while($select2 = mysql_fetch_array($_SESSION['select']))
{
$n1 = $select2[0];
$n2 = $select2[1];
$n3 = $select2[2]."-".$select2[3]."-".$select2[4];
echo
"<tr>
<td width=\"30px\"> $n1</td>
<td width=\"30px\"> $n2</td>
<td width=\"30px\"> $n3</td>
</tr>";
}
</div>
Each of your mysql_query()
lines is wrong. You should close the parentheses before adding or die()
.
This would be why you're getting a boolean instead of dieing with the error.
$_SESSION['select']= mysql_query("SELECT * FROM ".$_SESSION['dbtable3']." WHERE eyear = '".$_POST['year']."' AND emonth = '".$_POST['month']."' AND edate = '".$_POST['day']."' ;" or die(mysql_error()));
should be
$_SESSION['select']= mysql_query("SELECT * FROM ".$_SESSION['dbtable3']." WHERE eyear = '".$_POST['year']."' AND emonth = '".$_POST['month']."' AND edate = '".$_POST['day']."' ;") or die(mysql_error());
That is, the or die(mysql_error())
should be outside mysql_query
.
That, or you're SQL injecting yourself through your un-sanitized POST
parameters.
Yes, the error says everything
You do with
while($select2 = mysql_fetch_array($_SESSION['select']))
Use
$result = mysql_fetch_array($_SESSION['select']));
if($result) {
// go to while
} else {
mysql_error();
}
There is a problem in your query:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
Alto, there is a problem in your code: get rid of mysql_*
functions for PDO or msqli_*
, ad they are deprecated:
Suggested alternatives
Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
- mysqli_query()
- PDO::query()