查询未在函数中返回正确的结果

发布于 2024-12-11 11:39:17 字数 734 浏览 0 评论 0原文

以下函数设置为:

  1. 从我的数据库收集 user_id,如下面 WHERESubscriber = '$user' 所示。然后:
  2. 返回user_id可能的多个值。

但是,当我尝试使用此函数时,它只收集了一个数组,其中包含键 0 和值 user_id。出于明显的原因,我希望数组包含数据库中 user_id 列中带有键 user_id 的数值。

function get_subscribitions($user)
{
    $user = mysql_real_escape_string ($user);

    $sql = "SELECT 'user_id' FROM `subscribe` WHERE subscriber = '$user'";

    $result = mysql_query($sql);

    $rows = array();

    while ($row = mysql_fetch_assoc($result)) {
        $rows[] = $row;
    }

    mysql_free_result($result);

    return $rows;
}

谁能指出我在哪里犯了导致这些相关问题的错误?

任何帮助表示感谢,预先感谢您!

The following function is set up to:

  1. Collect the user_id from my database, as you can see below WHERE subscriber = '$user'. And then:
  2. return the possible multiple values that user_id are.

However, when I've tried to use this function, it has only collected an array, with the key 0, and value user_id. For apparent reasons I want the array to contain the numerical value that is in the user_id column in my database with the key user_id.

function get_subscribitions($user)
{
    $user = mysql_real_escape_string ($user);

    $sql = "SELECT 'user_id' FROM `subscribe` WHERE subscriber = '$user'";

    $result = mysql_query($sql);

    $rows = array();

    while ($row = mysql_fetch_assoc($result)) {
        $rows[] = $row;
    }

    mysql_free_result($result);

    return $rows;
}

Can anyone please pinpoint where I'm making the error leading to these related problems?

Any help appreciated, thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橙味迷妹 2024-12-18 11:39:17

应该是:

 function get_subscribitions($user)
{

$user = mysql_real_escape_string ($user);

  $sql = "SELECT user_id FROM `subscribe` WHERE subscriber = '$user'";

 $result = mysql_query($sql);

  $rows = array();

  while ($row = mysql_fetch_assoc($result)) {
      $rows[] = $row['user_id'];
  }

  mysql_free_result($result);

  return $rows;
}

查询中存在不必要的引号,并且您没有正确读取 $row 数组

It should be:

 function get_subscribitions($user)
{

$user = mysql_real_escape_string ($user);

  $sql = "SELECT user_id FROM `subscribe` WHERE subscriber = '$user'";

 $result = mysql_query($sql);

  $rows = array();

  while ($row = mysql_fetch_assoc($result)) {
      $rows[] = $row['user_id'];
  }

  mysql_free_result($result);

  return $rows;
}

There were unnecessary quotes in the query and you were not reading the $row array properly

贱贱哒 2024-12-18 11:39:17

尝试改为

$sql = "SELECT user_id FROM `subscribe` WHERE subscriber = '$user'";

注意 user_id 周围缺少单引号

Try instead

$sql = "SELECT user_id FROM `subscribe` WHERE subscriber = '$user'";

Notice the single quotes missing from around user_id

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文