努力使我的博客安全并学习准备好的语句。
虽然我设置了变量,但我仍然从数据库中获取所有条目。 $escapedGet 在我打印出来时是真正的变量。这显然是一个菜鸟错误,但我似乎找不到答案。
我需要获取 poSTLink 所在的数据 $escapedGet not all the data.
$escapedGet = mysql_real_escape_string($_GET['article']);
// Create statement object
$stmt = $con->stmt_init();
// Create a prepared statement
if($stmt->prepare("SELECT `title`, `description`, `keywords` FROM `post` WHERE `postlink` = ?")) {
// Bind your variable to replace the ?
$stmt->bind_param('i', $postlink);
// Set your variable
$postlink = $escapedGet;
// Execute query
$stmt->execute();
$stmt->bind_result($articleTitle, $articleDescription, $articleKeywords);
while($stmt->fetch()) {
echo $articleTitle, $articleDescription, $articleKeywords;
}
// Close statement object
$stmt->close();
}
刚刚尝试了这个:echo $escapedGet;
echo $_Get['artcile']
然后得到 - some_other 这就是我在数据库中保存为 poSTLink 的相同条目
尝试将 shande 后缀链接到 id,然后成功了。但为什么不使用 poSTLink 选项卡呢?
最佳答案
当您使用 'i'
修饰符绑定(bind)数据时,它会被绑定(bind)为整数。
意味着字符串将在最终语句中强制转换为 0。
但是由于 mysql 进行类型转换,您的字符串在此查询中变为零:
SELECT title FROM post WHERE postlink = 0;
试试看 - 对于文本后链接,您将返回所有记录(以及一堆警告)。
因此,使用 s
修饰符绑定(bind)字符串,而不是 i
关于mysql - 准备好的语句获取数据库中的所有结果,但不是我要求的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15521054/