我想從定義值中選擇所有行作爲相同的用戶名。當我嘗試從定義值中選擇所有行用戶名
$db->query("SELECT * FROM data WHERE username = $username")
它不工作,並得到「同時檢索數據時出現錯誤」。所以 我
$db->query("SELECT * FROM data WHERE(username='".intval($username)."')")
再試一次,它的工作找到,但我被困在獲得「數據庫爲空」
<?php
$response = array();
if(isset($_GET['username'])){
$username = $_GET['username'];
require_once __DIR__ . '/db_config.php';
$db = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
if(mysqli_connect_errno()){
printf("Connect failed: %S\n", mysqli_connect_error());
exit();
}
if($result = $db->query("SELECT * FROM data WHERE(username='".intval($username)."')"){
$rowCount = $result->num_rows;
if($rowCount > 0){
$response["data"] = array();
while($row = $result->fetch_assoc()){
$data = array();
$data["_id"] = $row["_id"];
$data["date"] = $row["date"];
$data["username"] = $row["username"];
$data["name"] = $row["name"];
$data["lastname"] = $row["lastname"];
$data["tel"] = $row["tel"];
array_push($response["data"], $data);
}
$response["success"]=1;
}
else{
$response["success"]=0;
$response["message"]="The database is empty.";
}
$result->close();
}
else{
$response["success"]=0;
$response["message"]="An error occurred while retrieving data.";
}
}
else{
$response["success"]=0;
$response["message"]="Required field(s) is missing.";
}
echo json_encode($response);
?>
[變量$用戶名必須用單引號 (')包圍]爲什麼你在查詢中使用'intval'? –
檢查此列名稱的數據類型應該是Char還是Varchar然後 '$ db-> query(「SELECT * FROM data WHERE username LIKE'%{$ username}%'」);' – Sundar
「SELECT * FROM data WHERE username =「。$ username; – JYoThI