【问题标题】:I want to use two where conditions in php to get data from mysql我想在 php 中使用两个 where 条件从 mysql 获取数据
【发布时间】:2016-06-15 13:59:38
【问题描述】:

我的问题是这样的:

<?php
    // Connect to database server
    mysql_connect("localhost", "root", "abcabc") or die (mysql_error ());

    // Select database
    mysql_select_db('iite') or die(mysql_error());

    // Get data from the database depending on the value of the id in the URL
    $strSQL = "SELECT * FROM table1 WHERE id=" . $_GET["id"];
    $rs = mysql_query($strSQL);

    // Loop the recordset $rs
    while($row = mysql_fetch_array($rs)) {

        // Write the data of the person
        echo'<h1>'. $row['title'].'</h1>';
        echo'<h1>'. $row['price'].'</h1>';
    }

    // Close the database connection
    mysql_close();
?>

我想显示相关帖子,为此我需要插入:

$sql = "SELECT * FROM table1 where title like '%keyword%' limit 5";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["price"]. " " . $row["title"]. "<br>";
    }
} else {
    echo "0 results";
}

那么我怎样才能在附近插入相关的帖子部分

$strSQL = "SELECT * FROM table1 WHERE id=" . $_GET["id"];

以及如何回显?

谢谢

【问题讨论】:

标签: php echo conditional-statements where


【解决方案1】:

您可以使用 OR 或 AND 在查询中组合不同的 where 条件(根据要求):

前;

$strSQL = "SELECT * FROM table1 WHERE id=" . $_GET["id"] ." OR title like '%keyword%' limit 5";

【讨论】:

    【解决方案2】:
    $strSQL = mysql_query(SELECT * FROM table1 WHERE id=" . $_GET["id"] ." OR title like '%keyword%' limit 5) or die(mysql_error());
    
    $row_strSQL = mysql_fetch_assoc($strSQL );
    $totalRows_strSQL = mysql_num_rows($strSQL );
    
    
    if ($result->totalRows_strSQL > 0) {
        // output data of each row
        while($row_strSQL= $result->fetch_assoc()) {
            echo "id: " . $row_strSQL["id"]. " - Name: " . $row_strSQL["price"]. " " . $row_strSQL["title"]. "<br>";
        }
    } else {
        echo "0 results";
    }
    

    【讨论】:

      【解决方案3】:

      您根本不需要两个单独的查询。只需在你的 where 子句中使用 OR 运算符,如 Where id=" . $_GET['id']." or title like '%keyword%'

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-10
        • 2020-10-06
        • 2011-08-03
        • 2020-03-19
        • 2022-01-24
        • 2021-10-09
        • 1970-01-01
        相关资源
        最近更新 更多