嵌套if循环部分工作

时间:2014-06-02 17:11:48

标签: php html mysql

我想要做的只是显示行值。现在假设该字段是' head_office'没有价值' H.O'然后我想显示最后一行的值。我试过但无法找到任何解决方案。这是我的代码:(我只是阻止了php部分)

 <?php
 $mysql_host = 'localhost';
 $mysql_user = 'root';
 $mysql_password = '123';
 $mysql_database = 'sdbms';
 $setup_page = './myinstitute.php';
 $db = mysql_connect($mysql_host, $mysql_user, $mysql_password);
 mysql_select_db($mysql_database, $db);


 if(isset($_REQUEST['id'])){
    $id=$_REQUEST['id'];
    $sql = "SELECT * FROM institute WHERE id =$id";
    $result = mysql_query($sql, $db);
    $row = mysql_fetch_array($result);
 }
 else if(!isset($_REQUEST['id'])){
    $sql = 'SELECT * FROM institute WHERE head_office ="H.O"';
    $result = mysql_query($sql, $db);
    $row = mysql_fetch_array($result);
 }
 else{
    $sql="SELECT * FROM institute";
    $result = mysql_query($sql, $db);
    $n = mysql_num_rows($result); //counting number of rows
    if($n==0){
        header('Location: '.$setup_page);
    }
    else{
        $sql = 'SELECT * FROM institute ORDER BY id DESC LIMIT 1';
        $result = mysql_query($sql, $db);
        $row = mysql_fetch_array($result);
    }
 }

?>

3 个答案:

答案 0 :(得分:2)

<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_password = '123';
$mysql_database = 'sdbms';
$setup_page = './myinstitute.php';
$db = mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $db);
$row = array();


if(isset($_REQUEST['id'])) {
    $id = (int) $_REQUEST['id'];
    if(!empty($id)) {
        $sql = "SELECT * FROM institute WHERE id =$id";
        $result = mysql_query($sql, $db);
        $row = mysql_fetch_array($result);
    }
} else {
    $sql = 'SELECT * FROM institute WHERE head_office = "H.O"';
    $result = mysql_query($sql, $db);
    $row = mysql_fetch_array($result);
}

if(!isset($_REQUEST['id']) && empty($row))
    $sql = "SELECT * FROM institute";
    $result = mysql_query($sql, $db);
    $n = mysql_num_rows($result); //counting number of rows

    if($n == 0) {
        header('Location: ' . $setup_page);
    } else {
        $sql = 'SELECT * FROM institute ORDER BY id DESC LIMIT 1';
        $result = mysql_query($sql, $db);
        $row = mysql_fetch_array($result);
    }
}
?>

答案 1 :(得分:2)

由于$_REQUEST['id']只能有2个status,isset和!isset,所以else语句永远不会被使用。

答案 2 :(得分:1)

我不太了解你想做什么,但它不合逻辑:这三步不会执行。试试吧:

if(isset($_REQUEST['id'])){
    $id=$_REQUEST['id'];
    $sql = "SELECT * FROM institute WHERE id =$id";
    $result = mysql_query($sql, $db);
    $row = mysql_fetch_array($result);
}
else if(!isset($_REQUEST['id'])){
    $sql = 'SELECT * FROM institute WHERE head_office ="H.O"';
    $result = mysql_query($sql, $db);
    $row = mysql_fetch_array($result);
}
if(count($row)<=0) {
    $sql="SELECT * FROM institute";
    $result = mysql_query($sql, $db);
    $n = mysql_num_rows($result); //counting number of rows
    if($n==0){
        header('Location: '.$setup_page);
    }
    else{
        $sql = 'SELECT * FROM institute ORDER BY id DESC LIMIT 1';
        $result = mysql_query($sql, $db);
        $row = mysql_fetch_array($result);
    }
}

享受您的代码。