2013-03-20 24 views
1

试图使我的博客安全并学习准备好的语句。准备语句获取db中的所有结果,但不是我要求的

尽管我设置了变量,但我仍然从数据库中获取所有条目。 $ escapedGet在打印出来时是真正的变量。这显然是一个新秀错误,但我似乎找不到答案。

我需要获取数据,其中postlink是$ escapedGet不是所有的数据。

$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(); 
     } 

只是tryed这一点:echo $escapedGet; echo $_Get['artcile']

,并得到 - some_other 那,我已经保存在数据库中postlink

试图善德postlink到ID,然后它的工作相同的条目。但为什么不与后链接选项卡?

+0

CANU告诉我表和$ escapedGet价值 – PSR 2013-03-20 10:37:45

+0

我试着使用$ _GET代替$ escapedGet [“文章”],同样的结果,然后我试着使用数据库的确切值,并且仍然得到所有结果。 – maarcs 2013-03-20 10:40:46

+0

尝试将一个静态值代替$ escapedGet,然后我们可以找到问题在哪里它是 – PSR 2013-03-20 10:41:47

回答

1

当您使用'i'修饰符绑定数据时,它将被绑定为整数。 表示字符串将在最后的语句中转换为0。

但作为MySQL的类型转换,你的字符串成为这个查询零:

SELECT title FROM post WHERE postlink = 0; 

尝试一下,看看 - 用于为文本postlinks你将有你所有返回的记录(以及一堆警告)。

因此,使用s修改,绑定字符串不i

+0

谢谢。这有帮助。 – maarcs 2013-03-20 11:11:10