mysql - 准备好的语句获取数据库中的所有结果,但不是我要求的结果

标签 mysql prepared-statement

努力使我的博客安全并学习准备好的语句。

虽然我设置了变量,但我仍然从数据库中获取所有条目。 $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/

相关文章:

python - 如何使用函数和 sqlalchemy 连接到数据库

php - 将图像上传到数据库(php)

JavaScript SQLite 准备好的语句

java - 在 java 中使用 PreparedStatement 的正确方法是什么?

mysql - 在另一个表上使用条件来使用 sum()

MYSQL从表1中选择第二个表的条件

php - Mysql - 如何获取连续预订一定天数的用户?

c# - 使用 C# 在 TextBox 中准备语句查询

PHP: stmt->execute() 失败,但错误为空

mysql - 执行语句是否总是为结果集占用内存?