2 回答

TA貢獻1828條經驗 獲得超4個贊
要檢查是否存在,您無需從數據庫中選擇任何數據。如果存在,您可以只獲取 1 否則它將返回NULL。此外,您應該始終記住使用準備好的語句,并且永遠不要將 PHP 變量注入 SQL 查詢。
$chat_id = 1234;
$stmt = $connection->prepare('SELECT 1 FROM cars WHERE chat_id = ?');
$stmt->bind_param('i', $chat_id);
$stmt->execute();
// fetch the first column from the first row as an integer
$return_id = (int) $stmt->get_result()->fetch_row()[0];

TA貢獻1836條經驗 獲得超4個贊
您可以使用mysqli_num_rows檢查記錄是否存在和WHERE子句來過濾結果。
$chat_id= 1234;
$query= "SELECT chat_id FROM cars WHRE chat_id = '$chat_id'";
$return_id= mysqli_query($connection, $query);
$rowcount = mysqli_num_rows($return_id);
echo $rowcount; // Will return total number of rows matched else will return zero
- 2 回答
- 0 關注
- 289 瀏覽
添加回答
舉報