【问题标题】:Trouble Getting Data From Row of mySQL to print out -PHP无法从 mySQL 行获取数据以打印出来 -PHP
【发布时间】:2013-07-14 22:38:43
【问题描述】:

我正在尝试获取某个用户的“目标”,该目标已存储在 MySQL 表的行中以及他们的“user_id”,以打印为列表。这是我一直在尝试的:

$user_id = (int)$session_user_id;
$result = mysql_query("SELECT user_id,goal1 FROM goals WHERE user_id  = $user_id");

if (!$result) {
echo 'Could not run query: '.mysql_error();
}
$row = mysql_fetch_row($result);

echo ($row[1]);
echo ($row[2]);
echo ($row[3]);
echo ($row[4]);
echo ($row[5]);

$session_user_id 是一个全局变量,包含登录用户的用户 ID。
每个用户只有五个目标。
有什么想法吗?

【问题讨论】:

  • echo $user_id; 先进行调试,然后在 PHPMyAdmin 上测试查询,然后,如果一切正常,如果您仍然...
  • 什么都没有了……可能导致这种情况的常见错误?
  • 没关系,它又在回响了。现在来测试查询。

标签: php mysql printing rows


【解决方案1】:

试试这个

    $user_id = (int)$session_user_id;
   $result = mysql_query("SELECT user_id,goal1 FROM goals WHERE user_id  = $user_id");

  if (!$result) {
  echo 'Could not run query: '.mysql_error();
 }
 while ($row = mysql_fetch_assoc($result)){

 echo $row['user_id'] ." ".$row['goal1']."<br />";

 }

【讨论】:

  • mysql_fetch_array 不返回关联数组,使用 mysql_fetch_assoc
【解决方案2】:

看起来您需要遍历行:

while ($row = mysql_fetch_assoc($result)) {
    echo $row['goal1'];
}

您还应该使用 mysql_fetch_assoc,因为 mysql_fetch_row 在 PHP 5.5.0 中将被弃用

【讨论】:

    猜你喜欢
    • 2013-07-02
    • 1970-01-01
    • 2014-05-16
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2013-08-28
    • 1970-01-01
    相关资源
    最近更新 更多