简体   繁体   English

如何从我的数据库获取发布数据-多次提取

[英]How can I get post data from my db - multiple fetch

<?php 
if ($username = $_GET['username']) 
{
   $sql = "SELECT * FROM posts WHERE username='$username'";
   $q = $db->prepare($sql);
   $q->execute();

   while($q->fetch(PDO::FETCH_ASSOC))
   {
      echo "$username<br/>";
   }
}
?>

Hello everybody. 大家好。 I wanna if username for ex Baran just fetch baran's posts. 我想问一下前Baran的用户名是否可以获取baran的帖子。 I have these code and I am getting usernames but when I try the get post date from posts table it does not work. 我有这些代码,正在获取用户名,但是当我尝试从posts表获取get date时,它不起作用。 What am I suppose to do. 我该怎么办。

i think its a typo 我认为这是一个错字

if ($username = $_GET['username']) {// assingment

change to: 改成:

if ($username == $_GET['username']) {//equal

use preparedStatement to avoid sql injection: 使用prepareStatement避免SQL注入:

<?php 
if ($username == $_GET['username']) {
   $sql = "SELECT * FROM posts WHERE username = :user ";
   $q = $db->prepare($sql);
   $q->bindValue(':user', $username);
   $q->execute();
   while($row = $q->fetch(PDO::FETCH_ASSOC)) {
      echo $row['username'] .'<br/>';
   }
}

Are you looking something similar? 您是否在寻找相似的东西? change if ($username == $_GET['username']) { 更改if ($username == $_GET['username']) {

while($row = $q->fetchAll(PDO::FETCH_ASSOC))
 {
  echo $row['username'];
 }

You have to assign the result of $q->fetch to some variable. 您必须将$q->fetch的结果分配给某个变量。 See fetch() documentation . 请参阅fetch()文档

In your example you just echo the value used as input. 在您的示例中,您仅回显了用作输入的值。

inside if you should always be using == 如果您应该始终使用==

eg:if($i ==1)
{
something here
}
else
{
something else
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM