Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_fetch_array() expects parameter 1 to be resource problem [duplicate]

Tags:

html

php

mysql

Possible Duplicate:
“Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given” error while trying to create a php shopping cart

I don't get it, I see no mistakes in this code but there is this error, please help:
mysql_fetch_array() expects parameter 1 to be resource problem

<?php

      $con = mysql_connect("localhost","root","nitoryolai123$%^");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("school", $con);
       $result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']);
    ?>     


                           <?php while ($row = mysql_fetch_array($result)) { ?>             
                                     <table class="a"  border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#D3D3D3">
    <tr>

    <form name="formcheck" method="get" action="updateact.php" onsubmit="return formCheck(this);">
    <td>
    <table  border="0" cellpadding="3" cellspacing="1" bgcolor="">
    <tr>

    <td  colspan="16" height="25"  style="background:#5C915C; color:white; border:white 1px solid; text-align: left"><strong><font size="2">Update Students</td>


    <tr>
    <td width="30" height="35"><font size="2">*I D Number:</td>
    <td width="30"><input  name="idnum" onkeypress="return isNumberKey(event)" type="text" maxlength="5" id='numbers'/ value="<?php echo $_GET['id']; ?>"></td>
    </tr>

    <tr>
    <td width="30" height="35"><font size="2">*Year:</td>
    <td width="30"><input  name="yr" onkeypress="return isNumberKey(event)" type="text" maxlength="5" id='numbers'/ value="<?php echo $row["YEAR"]; ?>"></td>

<?php } ?>

I'm just trying to load the data in the forms but I don't know why that error appears. What could possibly be the mistake in here?

like image 798
user225269 Avatar asked Apr 23 '10 09:04

user225269


3 Answers

You are not doing error checking after the call to mysql_query:

$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']);
if (!$result) { // add this check.
    die('Invalid query: ' . mysql_error());
}

In case mysql_query fails, it returns false, a boolean value. When you pass this to mysql_fetch_array function (which expects a mysql result object) we get this error.

like image 128
codaddict Avatar answered Oct 30 '22 17:10

codaddict


$id = intval($_GET['id']);
$sql = "SELECT * FROM student WHERE IDNO=$id";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql);

always do it this way and it will tell you what is wrong

like image 43
Your Common Sense Avatar answered Oct 30 '22 15:10

Your Common Sense


Give this a try

$indo=$_GET['id'];
$result = mysql_query("SELECT * FROM student WHERE IDNO='$indo'");

I think this works..

like image 41
yapiyapi Avatar answered Oct 30 '22 16:10

yapiyapi