1

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

This is the error I get: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in.....

I tried to find out the error , but I can't make it...can someone please help me take a look?

<?php

 session_start();


$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("EMC");


    $haha = $fgmembersite->UserEmail();
    $_SESSION['storedemail']= $haha;



    $query = mysql_query("SELECT * FROM Register WHERE Email= '$haha'");

    while ($row = mysql_fetch_array($query)){

    $id = $row['ID'];

    }
    mysql_free_result($query);



    $txtFirstName = isset($_GET['txtFirstName']) ? $_GET['txtFirstName'] : ""; 
    $txtLastName = isset($_GET['txtLastName']) ? $_GET['txtLastName'] : "";
    $txtCell = isset($_GET['txtCell']) ? $_GET['txtCell'] : "";

    ?>

    <html>


    <body>

    <form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
    <table width="599" border="1">
    <tr>
    <th>Keyword</th>

  <input name="txtFirstName" type="text" id="txtFirstName" value='<?php echo htmlentities($txtFirstName) ?>'>

  <input name="txtLastName" type="text" id="txtLastName" value='<?php echo htmlentities($txtLastName) ?>'>

  <input name="txtCell" type="text" id="txtCell" value='<?php echo htmlentities($txtCell) ?>'>



  <?

 $opselect="SELECT DISTINCT Custgroup FROM UserAddedRecord";

 $result = mysql_query ($opselect);

 while($nt=mysql_fetch_array($result))
 {
 ?>
 <option><?= $nt[Custgroup]?></option>";

 <?
 }
 ?>

 </select>
 <input type="submit" value="Search">
 </tr>
 </table>
</form>
<?
if (isset($_GET["txtFirstName"]) != "" || isset($_GET["txtLastName"]) != "" || isset($_GET["txtCell"]) != "" || isset($_GET["txtGroup"]) != "")
{
$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("EMC");
// Search By Name or Email
$strSQL = "SELECT * FROM UserAddedRecord WHERE (FirstName LIKE '%".$_GET["txtFirstName"]."%' and LastName LIKE '%".$_GET["txtLastName"]."%' and Cell LIKE '%".$_GET["txtCell"]."%' and Custgroup LIKE '%".$_GET["txtGroup"]."%')";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);


$Per_Page = 2;   // Per Page

    if(!isset($_GET['Page'])) 
        {
            $Page = 1;
        }
     else 
        {
            $Page = $_GET['Page'];
        }

$Prev_Page = $Page-1;
$Next_Page = $Page+1;

$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
    $Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
    $Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
    $Num_Pages =($Num_Rows/$Per_Page)+1;
    $Num_Pages = (int)$Num_Pages;
}


$strSQL .=" order  by addedrec_ID DESC LIMIT $Page_Start , $Per_Page";
$objQuery  = mysql_query($strSQL);

?>
<table width="600" border="1">
  <tr>
    <th width="50"> <div align="center">#</div></th>
    <th width="91"> <div align="center">CustomerID </div></th>
    <th width="98"> <div align="center">Name </div></th>
    <th width="198"> <div align="center">Email </div></th>
    <th width="97"> <div align="center">MobileCompany </div></th>
    <th width="59"> <div align="center">Cell </div></th>
    <th width="71"> <div align="center">WorkPhone </div></th>
  </tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
  <tr>
    <td><div align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?=$objResult["addedrec_ID"];?>"></div></td>
    <td><div align="center"><?=$objResult["addedrec_ID"];?></div></td>
    <td><?=$objResult["FirstName"];?></td>
    <td><?=$objResult["LastName"];?></td>
    <td><div align="center"><?=$objResult["MobileCompany"];?></div></td>
    <td align="right"><?=$objResult["Cell"];?></td>
    <td align="right"><?=$objResult["Workphone"];?></td>
  </tr>
<?
}
?>
</table>
<br>
Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
<?
if($Prev_Page)
{
       if($_GET["txtFirstName"] == "")
                  {
    echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtLastName=$_GET[txtLastName]'><< Back</a> ";    
                  }

        else
                  {
    echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtFirstName=$_GET[txtFirstName]'><< Back</a> ";
                  }

}

for($i=1; $i<=$Num_Pages; $i++){
    if($i != $Page)
    {

        echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtFirstName=$_GET[txtFirstName]'>$i</a> ]"; 

    }
    else
    {
        echo "<b> $i </b>";
    }
}
if($Page!=$Num_Pages)
{
          if($_GET["txtFirstName"] == "" && $_GET["txtLastName"] == "" && $_GET["txtCell"] == "")
                       {
            echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtGroup=$_GET[txtGroup]'>Next>></a> ";
                       }
          else if($_GET["txtFirstName"] == "" && $_GET["txtLastName"] == "")
                       {
            echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtCell=$_GET[txtCell]'>Next>></a> ";    
                       }
          else if($_GET["txtFirstName"] == "" )
                       {
        echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtLastName=$_GET[txtLastName]'>Next>></a> ";    
                       }
          else
                       {
            echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtFirstName=$_GET[txtFirstName]'>Next>></a> ";  }
                   }

mysql_close($objConnect);


}   

   else 
   {

$strSQL = "SELECT * FROM UserAddedRecord WHERE user_id='$id'";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);

$Per_Page = 2;   // Per Page

    if(!isset($_GET['Page'])) 
        {
            $Page = 1;
        }
    else 
        {
            $Page = $_GET['Page'];
        }

$Prev_Page = $Page-1;
$Next_Page = $Page+1;

$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
    $Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
    $Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
    $Num_Pages =($Num_Rows/$Per_Page)+1;
    $Num_Pages = (int)$Num_Pages;
}


$strSQL .=" order  by id DESC LIMIT $Page_Start , $Per_Page";
$objQuery  = mysql_query($strSQL);





?>
<table width="600" border="1">
  <tr>
    <th width="50"> <div align="center">#</div></th>
    <th width="91"> <div align="center">ID </div></th>
    <th width="198"> <div align="center">First Name </div></th>
    <th width="198"> <div align="center">Last Name </div></th>
    <th width="250"> <div align="center">Mobile Company </div></th>
    <th width="100"> <div align="center">Cell </div></th>
    <th width="100"> <div align="center">Workphone </div></th>
    <th width="100"> <div align="center">Group </div></th>
  </tr>
<?

    echo "<form name='form1' method='post' action=''>";

while($objResult = mysql_fetch_array($objQuery))
{


    echo "<tr>";
    echo "<td align='center'><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"$objResult[addedrec_ID]\"></td>";
    echo "<td>$objResult[addedrec_ID] </td>";
    echo "<td>$objResult[FirstName]</td>";
    echo "<td>$objResult[LastName] </td>";
    echo "<td>$objResult[MobileCompany] </td>";
    echo "<td>$objResult[Cell] </td>";
    echo "<td>$objResult[WorkPhone] </td>";
    echo "<td>$objResult[Custgroup] </td>";

   echo "</tr>";

}


     echo "<td colspan='7' align='center'><input name=\"delete\" type=\"submit\" id=\"delete\" value=\"Delete\">";

   if(isset($_POST['delete'])) // from button name="delete"
 {
   $checkbox = $_POST['checkbox']; //from name="checkbox[]"
     $countCheck = count($_POST['checkbox']);

    for($d=0;$d<$countCheck;$d++)
     {
         $del_id  = $checkbox[$d];

    $sql = "DELETE from UserAddedRecord where addedrec_ID = $del_id";

    $result2=mysql_query($sql);

     }
         if($result2)
     {  
             header('Location: customer-adddata.php');
         }
         else
         {
    echo "Error: ".mysql_error();
         }
 }

    echo "</form>";

?>
</table>
<br>
Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
<?
if($Prev_Page)
{
       if($_GET["txtFirstName"] == "")
                  {
    echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtLastName=$_GET[txtLastName]'><< Back</a> ";    
                  }

        else
                  {
    echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtFirstName=$_GET[txtFirstName]'><< Back</a> ";
                  }

}

for($i=1; $i<=$Num_Pages; $i++){
    if($i != $Page)
    {

        echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtFirstName=$_GET[txtFirstName]'>$i</a> ]"; 

    }
    else
    {
        echo "<b> $i </b>";
    }
}
if($Page!=$Num_Pages)
{
          if($_GET["txtFirstName"] == "" && $_GET["txtLastName"] == "")
                       {
            echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtCell=$_GET[txtCell]'>Next>></a> ";    
                       }
          else if($_GET["txtFirstName"] == "" )
                       {
        echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtLastName=$_GET[txtLastName]'>Next>></a> ";    
                       }
          else
                       {
            echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtFirstName=$_GET[txtFirstName]'>Next>></a> ";  }
                   }

mysql_close($objConnect);



   }






   ?>
  </body>
  </html>

Thanks in advance.

Problem solved.

Thanks for everyone replied my question , here is my error:

Notice: Unknown column 'id' in 'order clause' in .....

I forgot to change my id here:

    $strSQL .=" order  by id DESC LIMIT $Page_Start , $Per_Page";
$objQuery  = mysql_query($strSQL) or trigger_error(mysql_error());;

It should be:

    $strSQL .=" order  by addedrec_ID DESC LIMIT $Page_Start , $Per_Page";

Allow me to say thanks again and wish everyone a very good day .

Community
  • 1
  • 1
Irene Ling
  • 1,881
  • 3
  • 26
  • 41

4 Answers4

3

Your query has failed somehow, and returned a boolean FALSE instead of a statement handle. Add this to find out why:

$query = mysql_query(...);
if ($query === FALSE) {
   die(mysql_error());
}

Never assume a database operation was successful.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 4
    +1 for `Never assume a database operation was successful.`! – ComFreek Oct 20 '11 at 15:39
  • @ComFreek: Seems to be an all-too-common assumption for ANYTHING external to PHP. "I don't need to check curl/mysql/whatever return values, it always works in dev!" – Marc B Oct 20 '11 at 15:40
  • Thanks for your reply , I tried it , but it still showing me the same error . – Irene Ling Oct 20 '11 at 15:51
  • You've got multiple mysql_query calls in your sample code. You have to check **EACH** of them. – Marc B Oct 20 '11 at 15:53
  • @Irene: You've also got MAJOR sql injection holes in your code, so I **STRONGLY** suggest you visit http://bobby-tables.com to find out why this is BAD. – Marc B Oct 20 '11 at 16:00
  • Thank you so much Marc B , the error is found . I will take it as a lesson ,and yes , never assume a database operation was successful , will remember it. Really thanks . And wish you have a nice day . – Irene Ling Oct 20 '11 at 16:05
  • Okay I will Marc B , thanks for the link . – Irene Ling Oct 20 '11 at 16:08
1

You probably have an error in your query. Try doing

$query = mysql_query("SELECT * FROM Register WHERE Email= '$haha'") or trigger_error(mysql_error());

to see what the error is.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
0

Your query probably failed and returned FALSE. Add the following:

$query = mysql_query("SELECT * FROM Register WHERE Email= '$haha'");

if (!$query)
    die(mysql_error());

while ($row = mysql_fetch_array($query)){
daiscog
  • 11,441
  • 6
  • 50
  • 62
0

You must not be returning any results from that query. Check if the $query is a resource before you use mysql_fetch_array().

 if (! is_resource($query)) { die "Database returned nothing;" }
Michael
  • 309
  • 2
  • 3
  • 16
  • Thank you Mike , I tried it as well , but the error is still the same . – Irene Ling Oct 20 '11 at 15:52
  • I usually do something like this. `code`$query = "SELECT * FROM Register WHERE Email= '$haha'"; `code`$result = mysql_query($query); Then you can "echo $query" to see if if the string is set correctly, and even test it out by connecting to your MySQL server via SSH or PHPMyAdmin. – Michael Oct 20 '11 at 15:56
  • Thanks again Mike , the error is found , I checked my every query , I forgot that I have more than one query. Thank you and have a nice day . – Irene Ling Oct 20 '11 at 16:01