1

Hi when i run my code this proplem apper

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.5.0\www\List.php on line 28

this is my code

<?php
session_start();
?>

<html>
<link rel='stylesheet' type='text/css' href='Style.css'>
<title>Registration</title>
<body>



<?php
$S=$_POST['Sec'];
echo "<H4>Web Development a</H4>";
echo "<b>CS  <br>Registration Page </b><br><br><br><br>";
echo "<b>This is the students lists who are registered in section $S: <br><br>";
echo "<table border=1 width=50%><tr><td width='6'></td><td bgcolor='#E66C2C' width='150'><b><center><font 
color='#FFFFFF'>Name</center></td><td bgcolor='#E66C2C' width='100'><b><center><font 
color='#FFFFFF'>ID</center></td>". "<td bgcolor='#E66C2C' width='100'><b><center><font color='#FFFFFF'>E-Mail</center></td></tr>";


include('con_db.php');

$sql = "select * from students where Sec=$S ";
$result = mysql_query($sql);
$i=0;

while ($r = mysql_fetch_array($result)) {
$i++;
echo "<tr><td bgcolor='#eae5a7' width='6'>";
            echo "<b><center>".$i."</center><br>";
echo"</td>";
echo "<td  width='150'>";
            echo "<b><center>".$r[Fname]."</center><br>" ;
echo "</td>";
echo "<td width='100'>";
            echo "<b><center>".$r[ID]."</center><br>" ;
echo "</td>";
echo "<td width='100'>";
            echo "<b><center>".$r[mail]."</center><br>" ;
echo "</td></tr>";
echo "</font>";
}

echo"</table>";
echo "<form name='form' method='post' action='Registration_List.php'>";
?>

<br><br><center><input type='submit' id='send' value='Back'  style="color: #FFFFFF; background-color: #E66C2C; border-width: 1; border-style: 1" ></form>

<?php
echo "<form name='form' method='post' action='ass1.php'>";
?>
<input type='submit' id='send' value='Home Page'  style="color: #FFFFFF; background-color: #E66C2C; border-width: 1; border-style: 1"></form></center></td></tr></table>


</body>
</html>
4
  • Don't forget that you can mark an answer as accepted. You only need to click on the tick icon. That shows appreciation for the effort. Commented May 6, 2011 at 11:35
  • Homework: Try typing <script>top.location="http://www.google.com"</script> in your form and, once you fix the SQL query, ' OR '1'='1. Commented May 6, 2011 at 11:37
  • possible duplicate of mysql_fetch_array() expects parameter 1 to be resource, boolean given in select Commented Aug 3, 2012 at 9:37
  • mysql_query has been deprecated since version 5.5.0. Better to use mysqli_query that supports prepared statements and therefore is much more secure. Commented Jun 23, 2017 at 12:34

4 Answers 4

2

rewrite so this:

$result = mysql_query($sql);
$i=0;

while ($r = mysql_fetch_array($result)) {

becomes this:

if($result = mysql_query($sql)){
    $i=0;

   while ($r = mysql_fetch_array($result)) {
   ...
   }
} else {
   //do something with mysql_error()
}      
Sign up to request clarification or add additional context in comments.

1 Comment

while ($r = mysql_fetch_array($result)) { if ($r["Sec"]==1 && $s != 1) $s--; if ($r["Sec"]==2 && $s != 1) $u--; if ($r["Sec"]==3 && $s != 1) $m--; if ($r["Sec"]==4 && $s != 1 ) $t--; if ($r["Sec"]==5 && $s != 1) $w--; } ?>
1

Your mysql_query($sql) call is probably returning false. You need to see if your script managed to connect to your mysql server

From the manual : http://php.net/manual/en/function.mysql-query.php

if (!$result) {
    die('Invalid query: ' . mysql_error());
}

3 Comments

+1, but a query error would also cause a false return, so check the query as well as the connection. a swift echo type call to mysql_error() after mysql_query() should do the trick.
@Jeff Parker I was actually editing my post when you commented :)
No problem ... and you've already had your +1. Don't eat it all at once :)
0

Basically Error like Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given.. means that your query result is wrong. mysql_query instead of returning result returns false, which means your query is wrong.

Simple check what's wrong using:

$sql = "select * from students where Sec=$S ";
#                               V ADDED
$result = mysql_query($sql) or die( mysql_error() );

Comments

0

The error means you had an error in your sql, make sure you escape it before using it because you might become victim of sql injection.

$sql = "SELECT * FROM students WHERE seconds=%d";
$sql = sprintf($sql,mysql_real_escape_string($S));

make sure you have the correct data in $S;

$result = mysql_query($sql) or die("Error: ".mysql_error());

and this way you will know what is your exact error.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.