dystopia
dystopia

Reputation: 93

Exploding String Then Fetching from Database

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

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

Upvotes: 0

Views: 1896

Answers (4)

user1023319
user1023319

Reputation: 11

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.

Upvotes: 1

espradley
espradley

Reputation: 2148

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

Upvotes: 0

jeni
jeni

Reputation: 440

$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

Upvotes: 0

Michele
Michele

Reputation: 1488

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

Upvotes: 0

Related Questions