分解字符串然后从数据库中获取

发布于 2024-11-19 18:01:35 字数 1239 浏览 1 评论 0原文

我有一个包含用户 ID 的数组。我想分解该数组并通过该用户 ID 从数据库中获取所有内容。它仅获取每个用户的 1 个条目,而不是该用户的每个条目。我错过了什么吗?谢谢你!


编辑:抱歉这么稀疏,这是一个漫长的沮丧之夜,我会详细说明。

if ( $follower_array != "" ) 
{

  $followArray = explode(",", $follower_array);
  $followCount = count($followArray);   
  $i = 0;
  $display .= "<table>";

  foreach( $followArray as $key => $value ) 
  {

      $i++;
      $sql = mysql_query(" SELECT * FROM database WHERE mem_id='$value' 
                           ORDER BY bc_date ASC 
                           LIMIT 50
                         ") or die ("error!");

    while ( $row = mysql_fetch_assoc($sql) ) 
    {
      $mem_id  = $row['mem_id'];
      $content = $row['content'];
      $display .= "<td>' .$content. '</td></tr>";
    }

    display .= "</table>";
}

数据库表如下:

members|content
---------------
            id |id
follower_array |mem_id
               |content

下面的数组看起来像“4,5,6,7”等。

我在虚拟数据中设置了四个成员 ID。它检索 $followArray 中的

内容输出为

  • mem_id:1 - content
  • mem_id:2 - content
  • mem_id:3 - content
  • mem_id:4 - content

但随后停止。当每个用户有更多内容时,它只会检索每个成员的一个内容。谢谢,我希望我能解决这个问题。

I have an array that has userid's. I want to explode that array and fetch everything from the database by that userid. It fetches only 1 entry by each user, rather than every entry from that user. Am I missing something? Thank you!


Edit: Sorry for being so sparse, it's been a long night of frustration, i'll elaborate.

if ( $follower_array != "" ) 
{

  $followArray = explode(",", $follower_array);
  $followCount = count($followArray);   
  $i = 0;
  $display .= "<table>";

  foreach( $followArray as $key => $value ) 
  {

      $i++;
      $sql = mysql_query(" SELECT * FROM database WHERE mem_id='$value' 
                           ORDER BY bc_date ASC 
                           LIMIT 50
                         ") or die ("error!");

    while ( $row = mysql_fetch_assoc($sql) ) 
    {
      $mem_id  = $row['mem_id'];
      $content = $row['content'];
      $display .= "<td>' .$content. '</td></tr>";
    }

    display .= "</table>";
}

The Database tables are as follows:

members|content
---------------
            id |id
follower_array |mem_id
               |content

follwer array looks like "4,5,6,7" etc.

I have four member id's set in the dummy data. It retrieves those in $followArray

The output is

  • mem_id:1 - content
  • mem_id:2 - content
  • mem_id:3 - content
  • mem_id:4 - content

but then stops. It only retrieves one content per member when there are more for each user. Thanks I hope I cleared that up.

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

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

发布评论

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

评论(4

神魇的王 2024-11-26 18:01:36

这是因为 foreach 仅增加 $i 一次,尝试使用 $key 因为每次 foreach 循环运行时它都会增加自己它是数组中每个项目的索引。

That's because foreach only increases $i once, try using $key as it increseses himself everytime foreach loop runs as it is the index of each item in the array.

以为你会在 2024-11-26 18:01:36

可能您在错误的列上使用了 WHERE 条件。 $sql 表示从数据库中选择所有条目 ID = $VALUE,
查询中使用的 id 是条目 id,您必须将 id 更改为 user_id=$value

SELECT * FROM database WHERE user_id='$value' LIMIT 50

Probably you are using the WHERE condition on the wrong column. $sql says SELECT ALL FROM DATABASE WHERE ENTRY ID = $VALUE,
the id used in your query is the entry id, you have to change id to user_id=$value

SELECT * FROM database WHERE user_id='$value' LIMIT 50
第七度阳光i 2024-11-26 18:01:36
$searchSQL = "select * from database where id=$value'";
$result = mysql_query($searchSQL); 
while ($row = mysql_fetch_array($result))
 { 
  $id= $row['id'];
  $name=$row['name'];
  $phn=$row['phn'];
  $sal=$row['sal'];
 }

这将获取您的数据,您可以在要显示的位置回显

$searchSQL = "select * from database where id=$value'";
$result = mysql_query($searchSQL); 
while ($row = mysql_fetch_array($result))
 { 
  $id= $row['id'];
  $name=$row['name'];
  $phn=$row['phn'];
  $sal=$row['sal'];
 }

this will fetch your data and you can echo where you want to display

和我恋爱吧 2024-11-26 18:01:36

尝试将 mysql_fetch_array 更改为 mysql_fetch_assoc

另外,请确保数据库中有不止一行满足您的条件。您应该尝试使用像 navicat 或 mysql work bench 这样的程序直接查询数据库...

这应该有帮助...


更新:

输出是否会因为您缺少开始 tr 标记而倾斜?另外,您用单引号终止了以双引号开头的字符串......也修复了这个问题。

if ( $follower_array != "" ) {

  $followArray = explode(",", $follower_array);
  $followCount = count($followArray);   
  $i = 0;
  $display .= "<table>";

  foreach( $followArray as $key => $value ) {

    $i++;
    $sql = mysql_query("
        SELECT * 
          FROM database 
         WHERE mem_id='$value' 
      ORDER BY bc_date ASC 
         LIMIT 50
    ") or die ("error!");

    while ( $row = mysql_fetch_assoc($sql) ) {
      $mem_id  = $row['mem_id'];
      $content = $row['content'];

      $display .= "<td><tr>" .$content. "</td></tr>";
    }
    display .= "</table>";
}

Try changing mysql_fetch_array to mysql_fetch_assoc

Also, make sure there are more than one rows in the database that meet your criteria. You should try using a program like navicat or mysql work bench to query the database directly...

That should help...


Update:

Could the output be skewed because your missing the opening tr tag? Also, you had single quotes terminating a string that began with double quotes....fixed that too.

if ( $follower_array != "" ) {

  $followArray = explode(",", $follower_array);
  $followCount = count($followArray);   
  $i = 0;
  $display .= "<table>";

  foreach( $followArray as $key => $value ) {

    $i++;
    $sql = mysql_query("
        SELECT * 
          FROM database 
         WHERE mem_id='$value' 
      ORDER BY bc_date ASC 
         LIMIT 50
    ") or die ("error!");

    while ( $row = mysql_fetch_assoc($sql) ) {
      $mem_id  = $row['mem_id'];
      $content = $row['content'];

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