0

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.

2
  • Show us the code where you execute and read from the query. Also, please tell me you're not fetching that array from the database as well... Commented Jul 11, 2011 at 3:39
  • Just for fun: Why not use an IN clause? Commented Jul 11, 2011 at 3:44

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

I put the question up in a hurry. I am querying it from user_id. thank you for the response, though!
0
$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

Comments

0

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>";
}

3 Comments

Yeah, it is only returning one result. I typed up the question a little fast, but edited it. Everything in each database have an ID, and I'm retrieving by member ID, but it's only coming back with one result.
fetch_assoc hasn't worked. I don't know if I'm going blind from looking at this for the last 9 hours or what. in the database there are 10 per user, set up with unique ID's. But thank you for the help. I will check out mqsql work bench to make sure everything is fine tuned. cheers
Maybe you mysql is good then. But you are missing the starting tr tag. Could be causing it. Keep with assoc though ausr youll get weird indices sometimes when using fetch array since it gives you numeric indices and assoc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.