drw85135 2016-04-03 07:13
浏览 28

PHP单独如果其他声明

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

Where should I add an if else statement for name and age that runs the sql statement when either the name or age is selected? Name and Age is from different column but in the same table

  • 写回答

3条回答 默认 最新

  • dongzhang5006 2016-04-03 07:24
    关注
    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';
    }
    

    Then you could use it for URLs like:

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

    Just note that this is a very simplified example, you need to validate the input as well.

    EDIT: You should not use mysql* (deprecated) functions, use mysqli* or PDO instead. For more information about mysql* functions read answers posted on this question.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100