简体   繁体   中英

how to get to sql query in php

how can echo data by $sql And $sqll together can anybody solve this issue

$sql = "SELECT chat_msg, chat_date FROM chat WHERE chat_room_id = '13' AND userid = '13'";
$sqll = "SELECT chat_msg, chat_date FROM chat WHERE chat_room_id = '13' AND userid = '15'";
$result = $conn->query($sql);
$resultt = $conn->query($sqll);
if ($result->num_rows > 0 && $resultt->num_rows > 0){
    // output data of each row
    while($row = $result->fetch_assoc() && $roww = $resultt->fetch_assoc()) {
        echo "NO" . $row["chat_msg"]. $roww["chat_msg"].  "<br>";
    }
} else {
    echo "0 results";
}

Instead of merging the two results, you may be better off just querying all the relevant rows in a single query:

$sql = 
"SELECT chat_msg, chat_date FROM chat WHERE chat_room_id = '13' AND userid IN ('13', '15')";

Just put semicolon one after other query so its will return all rows in single query.

like that.

1 option ->

$sql = "SELECT chat_msg, chat_date FROM chat WHERE chat_room_id = '13' AND userid = '13';SELECT chat_msg, chat_date FROM chat WHERE chat_room_id = '13' AND userid = '15'";

2 option -> with in Operater ( The following SQL statement selects all rows that are userid in 13,15) more info check here https://www.w3schools.com/sql/sql_in.asp

$sql = "SELECT chat_msg, chat_date FROM chat WHERE chat_room_id = '13' AND userid IN ('13', '15')" ;

You have two queries ie-

SELECT chat_msg, chat_date FROM chat WHERE chat_room_id = '13' AND userid = '13'
SELECT chat_msg, chat_date FROM chat WHERE chat_room_id = '13' AND userid = '15'

Though this query will work fine, in case it can be optimized as follows -

SELECT chat_msg, chat_date FROM chat WHERE chat_room_id = '13' AND userid IN ('13','15');

Instead of two separate queries, you can use only one query for your requirement.

So, the problem that you are facing to print queries will also be solved and query will also get optimized.

您还可以使用:-

$sql = "SELECT chat_msg, chat_date FROM chat WHERE chat_room_id = '13' AND (userid = '13' OR userid = '15')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM