mysql_fetch_array()需要参数1

时间:2013-05-01 06:55:58

标签: php html mysql database fetch

我尝试使用第一页中的select选项显示数据库中的一些数据

但是当页面试图从数据库中检索数据时,我收到两个错误

错误是

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);

任何人都知道如何解决这个问题?

感谢

1 个答案:

答案 0 :(得分:0)

试试这个

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