Php Mysql SELECT查询1列等于1个变量

时间:2014-10-08 07:46:52

标签: php mysql variables select where

我已经在国内外投入了4个多小时的线程,似乎错过了一件简单的事情。

我试图让几个用户上传他们的新闻'到MYSQL 但我想只显示'新闻'将登录的用户名(userpost)附加到该行 $ current是登录用户的用户名,有效 示例A不会过滤掉不包含$ current用户的行 示例B不提供任何输出

所以我试过A:

$result = mysqli_query($con,"SELECT * FROM images_tbl");
//echo $current . "2" . $current;
while($row = mysqli_fetch_array($result)) {
    if ($row['userpost'] = '.$current.') {
    $num = 0;
    $num = $num + 1;
    $pic.$num = $row['images_path'];
    $h1 = $row['hlone'];

和B:

 $result = mysqli_query($con,"SELECT * FROM images_tbl WHERE (userpost = '.$current.')");
    echo $current . "2" . $current;
    while($row = mysqli_fetch_array($result)) {
      echo $row['hlone'] . " " . $row['images_path'];
      echo "<img src=\"" .$row['images_path']. "\">";
    }

27,images / 08-10-2014-1412752801.jpg(images_path),2014-10-08,标题(hlone),标题2,故事,testb(用户信息)

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

在查询中添加where子句

//in situation A
$result = mysqli_query($con,"SELECT * FROM images_tbl where username='".$current."'");
//username is column name for user you might have to change this
while($row = mysqli_fetch_array($result)) {
    echo $row['images_path'];
    echo $row['hlone'];
}

在情况B中尝试这个

$result = mysqli_query($con,"SELECT * FROM images_tbl WHERE userpost = '".$current."')");
    echo $current . "2" . $current;
    while($row = mysqli_fetch_array($result)) {
      echo $row['hlone'] . " " . $row['images_path'];
      echo "<img src=\"" .$row['images_path']. "\">";
    } 

答案 1 :(得分:0)

为什么要尝试使用PHP进行过滤。

如果你想过滤当前用户没有写的'新闻',只需使用MySQL Where子句:

// For Example A
$result = mysqli_query($con, "SELECT * FROM images_tbl WHERE userpost != '{$current}'");
while($row = mysqli_fetch_array($result)) {
    $pic = $row['images_path'];
    $h1 = $row['hlone'];
}

// For Example B
$result = mysqli_query($con,"SELECT * FROM images_tbl WHERE userpost = '{$current}')");
echo $current . "2" . $current;
while($row = mysqli_fetch_array($result)) {
    echo $row['hlone'] . " " . $row['images_path'];
    echo "<img src=\"" .$row['images_path']. "\">";
} 

使用MySQL的过滤选项很容易。你应该做更多关于MySQL的研究。