好的,我正在尝试查询数据库,以找到name
列中与id
列中的值对应的值。所以我有id,我用它来获取值。出于某种原因,这似乎不起作用。回显名称不会打印出与该ID对应的名称,而是打印出1
,并且我也没有收到任何错误。
$con = new mysqli('localhost', 'username', 'pass', 'database_name');
$id = htmlspecialchars($_GET["id"]);
$query = 'SELECT name FROM files
WHERE id=' .$id;
$query_p = $con->prepare($query);
$name = $query_p->execute();
$con->close;
echo $name;
答案 0 :(得分:1)
$name = $query_p->execute(); // This won't give you the name back
执行()不会返回这些值。
成功时返回TRUE,失败时返回FALSE。
你不认为你必须fetch
结果吗?
$query_p = $con->prepare($query);
$query_p->execute();
$query_p->bind_result($name);
while ($stmt->fetch()) {
printf ("%s \n", $name);
}
答案 1 :(得分:1)
$db = new mysqli('localhost', 'username', 'pass', 'database_name');
$sql = 'SELECT name FROM files WHERE id=' . $_GET["id"];
$result = $db->query($sql);
//...
$row = $result->fetch_assoc();
$name = $row['name'];
答案 2 :(得分:0)
你能试试吗,
$id = $_GET["id"];
$query = 'SELECT name FROM files WHERE id="'.$id.'"' ;
$query_p = $con->prepare($query);
$sth = $query_p->execute();
while( $row = $sth->fetchAll()){
echo $row['name'];
}