mysql_fetch_array()期望参数1是资源错误

时间:2011-10-20 15:30:27

标签: php mysql

  

可能重复:
  mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

这是我得到的错误: 警告:mysql_fetch_array()期望参数1是资源,在.....中给出布尔值

我试图找出错误,但我无法做到......有人可以帮我看看吗?

<?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>

提前致谢。

问题解决了。

感谢大家回复了我的问题,这是我的错误:

注意:.....中的'order clause'中的未知列'id'

我忘了改变我的身份:

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

应该是:

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

请允许我再次表示感谢,祝大家度过美好的一天。

4 个答案:

答案 0 :(得分:3)

您的查询以某种方式失败,并返回一个布尔值FALSE而不是语句句柄。添加此内容以找出原因:

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

永远不要假设数据库操作成功。

答案 1 :(得分:1)

您的查询可能有错误。尝试做

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

查看错误是什么。

答案 2 :(得分:0)

您的查询可能失败并返回FALSE。添加以下内容:

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

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

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

答案 3 :(得分:0)

您不得返回该查询的任何结果。在使用mysql_fetch_array()之前检查$ query是否是资源。

 if (! is_resource($query)) { die "Database returned nothing;" }