2011-10-20 56 views
1

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in selectmysql_fetch_array()預計參數1是資源錯誤

這是我的錯誤: 警告: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> 

在此先感謝。

問題已解決。

感謝大家回答我的問題,這是我的錯誤:

注意:「以條款」未知列「ID」中.....

我忘了這裏改變我的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"; 

讓我送的再次感謝並祝大家度過愉快的一天。

回答

3

您的查詢以某種方式失敗,並返回布爾值FALSE而不是語句句柄。添加這個以找出原因:

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

永不假定數據庫操作成功。

+4

+1 for'永不假定數據庫操作成功.'! – ComFreek

+0

@ComFreek:似乎對於PHP以外的任何東西都是一種常見的假設。 「我不需要檢查curl/mysql /無論返回值,它總是在開發中工作!」 –

+0

感謝您的回覆,我試過了,但仍然顯示出同樣的錯誤。 –

1

您的查詢中可能有錯誤。試試做

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

看看錯誤是什麼。

+0

非常感謝你,我試過了,但它仍然顯示出同樣的錯誤。 –

+0

問題解決了,我知道現在有什麼錯誤。非常感謝Xeon06。願你有個愉快的一天 。 –

+0

@Irene,別忘了發佈它是什麼,並通過點擊旁邊的綠色複選標記來接受最有用的答案。 –

0

您的查詢可能失敗並返回FALSE。添加以下內容:

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

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

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

謝謝daiscog,添加你的代碼後錯誤仍然是一樣的。 –

+0

謝謝daiscog,我知道錯誤已經發生了,再次感謝,祝你有個美好的一天。 –

0

您不得從該查詢返回任何結果。在使用mysql_fetch_array()之前,檢查$ query是否是資源。

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

謝謝邁克,我也試過,但錯誤仍然是一樣的。 –

+0

我通常做這樣的事情。 'code' $ query =「SELECT * FROM Register WHERE Email ='$ haha​​'」; 'code' $ result = mysql_query($ query); 然後,您可以「echo $ query」來查看該字符串是否設置正確,甚至通過SSH或PHPMyAdmin連接到您的MySQL服務器來進行測試。 – Mike

+0

再次感謝Mike,發現錯誤,我檢查了我的每個查詢,我忘記了我有多個查詢。謝謝你,祝你有美好的一天 。 –

相關問題