我想从定义值中选择所有行作为相同的用户名。当我尝试
时$db->query("SELECT * FROM data WHERE username = $username")
它无法正常运行并且#34;检索数据时发生错误"。所以 我再试一次
$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);
?>
答案 0 :(得分:1)
intval究竟做了什么?
使用指定的基数返回var的整数值 转换(默认值为10)。不应该使用intval() 对象,因为这样做会发出E_NOTICE级别错误并返回1.
因此,您的查询与执行
大致相同SELECT * FROM data WHERE (username=0)
当然,因为你没有任何'0'的用户名,所以你没有得到任何匹配。
问题的症结在于:
$db->query("SELECT * FROM data WHERE(username='".intval($username )."')"
您不应该传递这样的参数。你应该这样做:
$stmt = $db->prepare("SELECT * FROM data WHERE username=?")
$stmt->bind_param("s, $username)
if($db->execute()) }
// and read this on how to fetch
// http://php.net/manual/en/mysqli-stmt.fetch.php
}
答案 1 :(得分:-1)
尝试这个[变量$ username必须用单引号括起来 (')]
<add name="CaseStudyRule" virtualUrl="^~/Case-Study/(.*)" destinationUrl="~/Case-Study.aspx?Id=$1"
rewriteUrlParameter="ExcludeFromClientQueryString"
ignoreCase="true" />
</rewrites>