【问题标题】:Php Mysql SELECT query 1 column equals 1 variablePhp Mysql SELECT 查询 1 列等于 1 个变量
【发布时间】:2014-10-08 07:46:52
【问题描述】:

我已经在国内外抛出了 4 个多小时的大量线程,并且似乎遗漏了一件简单的事情。

我正在尝试让几个用户将他们的“新闻”上传到 MYSQL。
然而,我只想显示带有登录用户名(userpost)的“新闻”附加到该行。
$current 是登录用户的用户名,有效。
示例 A 没有过滤掉不包含 $current 用户的行。
示例 B 未提供任何输出

所以我两个都试过了:

$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'];

和乙:

 $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, Headline(hlone), Headline2, story, testb(userpost)

任何帮助将不胜感激。

【问题讨论】:

  • 显示你的数据库模型

标签: php mysql variables select where


【解决方案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']. "\">";
    } 

【讨论】:

  • 抱歉耽搁了,我已经一次又一次地尝试了大家的建议,它仍然无法从数据库中回显任何信息......我尝试过的 WHERE 语句有很多变体并且无法弄清楚为什么它不起作用。
【解决方案2】:

您为什么要尝试使用 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 做更多的研究。

【讨论】:

  • 感谢大家的努力,仍然没有运气让这个工作。我知道 PHP 不是执行此操作的有效方法,但值得一试;示例 A 是唯一一个仍然从数据库中输出任何内容的示例,太糟糕了,它正在输出所有内容。我在 Mysql 上阅读了更多小时,并且有一本 OReilly Mysql 书,其中只有一些关于 WHERE 语句的短句。如果有人有任何想法或知道还有什么可能会丢失,那就太好了。非常感谢您的帮助:)
  • 这是一个迟到的答案,但您说$current 具有正确的值。再次,如果它仍然从数据库中获取所有数据,首先使用 phpMyAdmin 检查表的userpost 列,看看它是否有值。您需要自己进行一些调试。如果查询从 DB 获得结果,这意味着查询工作,所以我们不能从 MySQL 得到任何错误。 userpost 列中一定有问题。
猜你喜欢
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
  • 2017-10-26
相关资源
最近更新 更多