-2

Can anyone help me to figure out the error in my code? i want to display data through two input search..my code goes like these.

<table>
    <tr>
        <td>Studid</td>
        <td>Course</td>
    </tr>

 <?php
    include ("connect.php");


    if(isset($_POST['submit']))

    {
    $studno=$_POST['idsearch'];
    $scourse=$_POST['coursesearch'];
    $sql=mysql_query("SELECT * FROM cfnr WHERE studid= ".$studno." AND course=".$scourse."");

    }

    ?>


    <?php
    while($row=mysql_fetch_array($sql))

    {


 }  

?> 
  <tr>
        <td><?php echo $row['studid'];?></td>
        <td><?php echo $row['course'];?></td>
    </tr>



</table>

i got this error in my screen.

"mysql_fetch_array() expects parameter 1 to be resource, boolean given in "

thanks! :)

  • ignoring the deprecated library (use `mysqli_*` or PDO) and the SQL Injection vulnerability the error tells you the problem. `$sql` is a boolean. that means there's something wrong with your connection or your query. – Memor-X Aug 31 '16 at 04:00
  • Check $result before passing it to mysql_fetch_array. You'll find that it's false because the query failed – Drop Shadow Aug 31 '16 at 04:00
  • 1
    Possible duplicate of [mysqli\_fetch\_array()/mysqli\_fetch\_assoc()/mysqli\_fetch\_row() expects parameter 1 to be resource or mysqli\_result, boolean given](http://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc-mysqli-fetch-row-expects-parameter-1) – scrowler Aug 31 '16 at 04:01
  • hey guys do i have to change it? – Oman Reyes Dela Cruz Aug 31 '16 at 04:06

5 Answers5

-1

You should use a bit of debugging. Try this:

$sql   =  "SELECT * FROM cfnr WHERE studid= '".$studno."' AND course = '".$scourse."'";
$query =  mysql_query($sql) or die(__LINE__."has an error: ".mysql_error());  // Gives an error if there's a syntax error

$row = mysql_fetch_array($query);

echo "<pre>";
print_r($row);
exit;

while ($row = mysql_fetch_array($query)) {
     /* Other code */
}
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
-1

Replace your sql with below line :

$sql=mysql_query("SELECT * FROM cfnr WHERE studid= ".$studno." AND course=".$scourse."") or die(mysql_error());
Ravi Roshan
  • 570
  • 3
  • 14
  • i did it sir but it displays the same error, undefined variable in while($row=mysql_fetch_array($sql)) and mysql_fetch_array() expects parameter 1 to be resource, null given in. – Oman Reyes Dela Cruz Aug 31 '16 at 04:18
-1

The issue is your `$sql variable is within the if condition and if your if condition fails there is no scope of $sql outside of if condition

Instead of

while($row=mysql_fetch_array($sql))

Use

while($row=@mysql_fetch_array($sql))

you will not get this error.

PBajpai
  • 1
  • 2
-1

("SELECT * FROM cfnr WHERE studid= '$studno' AND course ='$scourse.'") or die(mysql_error());

castaway13
  • 33
  • 4
-1

Change the code :

while($row=mysql_fetch_array($sql))

to

while($row=mysql_fetch_assoc($sql))

and also change this code:

 $sql=mysql_query("SELECT * FROM cfnr WHERE studid= ".$studno." AND course=".$scourse."");

to

$sql=mysql_query("SELECT * FROM cfnr WHERE studid= '$studno' AND course='$scourse'");

Not only that, the close brace of the isset() should be on before your (?>) last closing php tag.

Change the code :

include ("connect.php");

to

include 'connect.php';
FullStack
  • 198
  • 7
  • 16