分解字符串然后从数据库中获取
我有一个包含用户 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为
foreach
仅增加$i
一次,尝试使用$key
因为每次foreach
循环运行时它都会增加自己它是数组中每个项目的索引。That's because
foreach
only increases$i
once, try using$key
as it increseses himself everytimeforeach
loop runs as it is the index of each item in the array.可能您在错误的列上使用了 WHERE 条件。 $sql 表示从数据库中选择所有条目 ID = $VALUE,
查询中使用的 id 是条目 id,您必须将 id 更改为 user_id=$value
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
这将获取您的数据,您可以在要显示的位置回显
this will fetch your data and you can echo where you want to display
尝试将 mysql_fetch_array 更改为 mysql_fetch_assoc
另外,请确保数据库中有不止一行满足您的条件。您应该尝试使用像 navicat 或 mysql work bench 这样的程序直接查询数据库...
这应该有帮助...
更新:
输出是否会因为您缺少开始 tr 标记而倾斜?另外,您用单引号终止了以双引号开头的字符串......也修复了这个问题。
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.