2016-04-03 345 views
0
<?php 
    extract($_GET);  
    $sql = "SELECT * FROM tablename order by Name DESC"; 
    $sql = "SELECT * FROM tablename where age = "31"; 
    $db = mysql_connect("localhost","root","password"); 
    if (!$db) { 
    die(""); 
    } 
    $db_select = mysql_select_db('databasename',$db); 
    if (!$db_select) { 
    die(""); 
    } 
    if (!($result = mysql_query($sql, $db))) { 
    print("Could not execute query! <br />"); 
    die(mysql_error() . "</body></html>"); 
    } // end if 

    echo "<table> 
    <tr> 
    <th>Name</th> 
    <th>Age</th> 
    </tr>"; 

    while($row = mysql_fetch_array($result)){ 
     echo "<tr>"; 
     echo "<td>".$row['Name']."</td>"; 
     echo "<td>".$row['Age']."</td>"; 
    } 
    echo "</table>"; 
    mysql_close($db); 
?> 

在選擇名稱或年齡時,我應該在哪裏添加運行sql語句的name和age的if else語句?姓名和年齡是從不同的列,但在同一個表PHP分離如果其他語句

+1

你的意思我們需要運行這些$ sql中的任何一個?請妥善澄清你的問題。 –

+0

要在選擇名稱或年齡時運行sql語句 – Synetrix

+0

好的,以及可能的查詢字符串鍵是什麼?正如$ _GET ['name']和$ _GET ['age']? –

回答

1
if(isset($_GET['age'])) { 
    $sql = 'SELECT * FROM tablename where age = ' . $_GET['age']; 
} else if(isset($_GET['name'])) { 
    $sql = 'SELECT * FROM tablename order by Name DESC'; 
} 

然後,你可以使用它的URL,如:

  • www.example.com?age=31
  • www.example。 com?name

請注意,這是一個非常簡單的例子,您還需要驗證輸入。

編輯:你不應該使用mysql *(不建議使用)的功能,而應該使用mysqli *或PDO。有關mysql *函數的更多信息,請閱讀this question上發佈的答案。

+1

在PHP 7中,'mysql_'函數實際上被**刪除**,並且在去年12月發佈。 – Arjan

0

看起來您需要檢查$ _GET的密鑰。

試試這個:

if (isset($_GET['Name']) && !empty($_GET['Name'])) { 
    $sql = "SELECT * FROM tablename order by Name DESC"; 
} else if (isset($_GET['Age']) && !empty($_GET['Age'])) { 
     $sql = "SELECT * FROM tablename where age = '".$_GET['Age']."'"; 
} 

希望這有助於。上述

+0

它給了我這個錯誤: 解析錯誤:語法錯誤,意外的'&&'(T_BOOLEAN_AND),期待','或')' – Synetrix

+0

第3行缺少'') – Rasclatt

+0

也只是一個附註,如果你正在檢查'empty',則需要使用'isset'。你只需要'if(!空($ _ GET ['Age']))' – Rasclatt

0
<?php 
    error_reporting(-1); 

    $db = mysqli_connect("localhost","root","password","databasename"); 
    if (!$db) { 
     die(mysql_error() . "</body></html>"); 
    } 

    if(isset($_GET['age'])) { 
     // You can use the $_GET['age'] variable in the query if you want to, this makes you vulnerable to sql injection though. if you don't use prepared statements or escape it (read link below 
     $sql = 'SELECT * FROM tablename where age = "31"'; 
    } else if(isset($_GET['name'])) { 
     // Same as for age but $_GET['name'] in this case of course. 
     $sql = 'SELECT * FROM tablename order by Name DESC'; 
    } 

    $result = mysqli_query($sql, $db) 
    if (!result) { 
     print("Could not execute query! <br />"); 
     die(mysql_error() . "</body></html>"); 
    } // end if 

    echo "<table> 
    <tr> 
    <th>Name</th> 
    <th>Age</th> 
    </tr>"; 

    while($row = mysqli_fetch_array($result)){ 
     echo "<tr>"; 
     echo "<td>".$row['Name']."</td>"; 
     echo "<td>".$row['Age']."</td>"; 
     echo "</tr>"; 
    } 
    echo "</table>"; 
    mysqli_close($db); 
?> 

的查詢是保存,只要你不使用在查詢本身的$ _GET變量,如果這是你想你應該準備的語句讀了一下:http://php.net/manual/en/mysqli.prepare.php