【问题标题】:Display text when MySQL Column is 1MySQL Column 为 1 时显示文本
【发布时间】:2012-11-08 04:49:27
【问题描述】:

所以这就是我所拥有的......问题是它给了我错误,我无法让它为我的生活工作!

<?php 
$link = mysql_connect($hostname, $username, $password);
mysql_select_db($database); 
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM messages WHERE reciever=$user");
while($row = mysql_fetch_assoc($result))
{
if($row['recieved'] == '1')
{
echo '<a class="new-message" title="New Message">New</a>';
}
else
{
echo '&nbsp;';
}
}
?>

$user 是会话用户名(来自不同的代码页)

我想要的是它只在尚未阅读 (=0) 时显示新消息,如果已阅读 (=1) 则显示一个空格(或不显示任何内容)。

【问题讨论】:

  • 不明白你的问题。把一些细节说得很清楚。

标签: php mysql displaytag private-messaging


【解决方案1】:

试试这个代码

<?php 
$link = mysql_connect($hostname, $username, $password);
mysql_select_db($database); 
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM messages WHERE reciever=$user");
$row = mysql_fetch_row($result);
echo '<a class="new-message" title="New Message">New</a>';
?>

你的代码是不正确的方式,因为任何时候你只想要一个用户数据而不是为什么使用 while 循环?

【讨论】:

  • 我喜欢您的代码,但就像上面的代码一样,当我尝试使用它时,无论我如何连接到 MySQL,我都会收到此错误:警告:mysql_fetch_row() 期望参数 1 是资源,布尔值在第 76 行的 /home/public_html/admin/inbox_list.php 中给出
【解决方案2】:
   $result = mysql_query("SELECT * FROM messages WHERE reciever=$user");
  while($row = mysql_fetch_assoc($result))
    {
   if($row['recieved'] == 1)
    {
          echo '<a class="new-message" title="New Message">New</a>';
    }
    else
    {
         echo '&nbsp;';
    }
    }

尝试使用它,看看是否有帮助

【讨论】:

  • 当我尝试使用它时,无论我如何连接到 MySQL,我都会收到此错误:警告:mysql_fetch_row() 期望参数 1 是资源,在 /home/public_html/admin/inbox_list 中给出的布尔值.php 在第 76 行
  • 使用 fetch_assoc,这意味着查询没有返回任何结果,请检查您的数据库列中的正确名称标题
猜你喜欢
  • 2017-01-07
  • 2021-03-13
  • 2020-09-07
  • 2020-03-30
  • 1970-01-01
  • 2019-07-07
  • 2015-09-03
  • 2022-01-11
  • 1970-01-01
相关资源
最近更新 更多