2013-05-01 68 views
-1

我試圖從第一頁mysql_fetch_array()預計參數1

顯示從數據庫中選擇選項的一些數據,但我有兩個錯誤時的頁面試圖從數據庫

的錯誤檢索數據

mysql_num_rows() expects parameter 1 to be resource, boolean given 

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

這是我的代碼

<?php 
//connect to server 
$connect = mysql_connect("localhost", "root", ""); 

//connect to database 
//select the database 
mysql_select_db("fak_databases"); 
//submit button 
if($_POST['formSubmit'] == "Submit") 
{ 
    $country = $_POST['country']; 
} 

//query the database 
if($country == TRUE) { 

    $order = ""; 
    $sort = "asc"; 
    $sql = "SELECT wipo_applicant1_city, applicant1_addr1 WHERE applicant1_country='$country' FROM auip_wipo_sample"; 
    if(isset($_GET['orderby'])){ 
     $order = $_GET['orderby']; 
     $sort = $_GET['sort']; 

     //limiting the possible values of order/sort variables 
     if($order != 'wipo_applicant1_city' && $order != 'applicant1_addr1')$order = "applicant1_addr1"; 
      if($sort != 'asc' && $sort != 'desc')$sort = "asc"; 
       $sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE applicant1_country='$country' ORDER BY ".mysql_real_escape_string($order)." ".$sort; 

       //here we reverse the sort variable 
       if($sort == "asc"){ 
        $sort = "desc"; 
       } 
      else{ 
       $sort = "asc"; 
      } 
     } 

} 

    $result = mysql_query($sql); 
    $num_rows = mysql_num_rows($result); 
    $row_counter = 0; 

    $icon = ""; 
    echo "<table border=\"1\" cellspacing=\"0\">\n"; 
    echo "<tr>\n"; 

    // first column 
    echo "<th>"; 
    $icon = ""; 
    if($order == "wipo_applicant1_city"){ 
     if($sort == "asc"){ 
      $icon = "<img src=\"images/up.png\" class=\"arrowSpace\"/>"; 
     } 
     if($sort == "desc"){ 
      $icon = "<img src=\"images/down.png\" class=\"arrowSpace\"/>"; 
     } 
    } 

    //print the result 
    echo "<a href='index.php?orderby=wipo_applicant1_city&sort=".$sort."'>City</a>".$icon; 
    echo "</th>\n"; 


    // second column 
    echo "<th>"; 
    $icon = ""; 
    if($order == "applicant1_addr1"){ 
     if($sort == "asc"){ 
      $icon = "<img src=\"images/up.png\" class=\"arrowSpace\"/>"; 
     } 
     if($sort == "desc"){ 
      $icon = "<img src=\"images/down.png\" class=\"arrowSpace\"/>"; 
     } 
    } 
    echo "<a href='index.php?orderby=applicant1_addr1&sort=".$sort."'>Address</a>".$icon; 
    echo "</th>\n"; 
    echo "</tr>"; 

//fetch the result 

while($row = mysql_fetch_array($result)) 
{ 
    if($row_counter % 2){ 
      $row_color="bgcolor='#FFFFFF'"; 
     }else{ 
      $row_color="bgcolor='#F3F6F8'"; 
     } 
    echo "<tr class=\"TrColor\" ".$row_color.">"; 
    echo "<td>" . $row['wipo_applicant1_city'] . "</td>\n"; 
    echo "<td>" . $row['applicant1_addr1'] . "</td>\n"; 
    echo "</tr>"; 
    $row_counter++; 
} 

Print "</table>"; 
?> 

行有錯誤是

$num_rows = mysql_num_rows($result); 

while($row = mysql_fetch_array($result)) 

我想我已經給了參數在此行

$result = mysql_query($sql); 

人知道如何解決這一問題?

感謝

+0

第一不要使用mysql_ *使用mysqli_ *和第二檢查mysql_num_rows($結果)> 0 – 2013-05-01 06:58:22

+1

你沒有做任何錯誤的數據庫連接或任何檢查查詢。 **這是一種犯罪。**那樣做。 – Mat 2013-05-01 06:59:08

+0

'mysql_query($ sql);'如果查詢失敗則返回false。所以$結果是錯誤的! – Waygood 2013-05-01 06:59:31

回答

0

試試這個

$sql = "SELECT wipo_applicant1_city, applicant1_addr1 FROM auip_wipo_sample WHERE  
     applicant1_country='".$country."'"; 
相關問題