3

i can't get multiple rows into array from mysql database? i have code but it is not working or not showing all rows when i echo into textbox?

<?php 
if(is_array($_SESSION['pid']))
{
  $pid = join(',',$_SESSION['pid']); 
  $result=mysql_query("SELECT id AS wid FROM mywishlist where pid='$pid'") 
            or die("Id    Problem"."<br/><br/>".mysql_error());
  $results= array();
  $i=0; // add the new line
  while($row=mysql_fetch_array($result)){
    $results[$i] = $row['wid'];
    $i++;
  }
  $results;
}
$max=count($results);
for($j=0; $j<$max; $j++)
{
?>
<input type="text" name="wid[]" value="<?php echo $results[$j]; ?>" />
<?php } ?>
9

3 Answers 3

2

The line join(',',$_SESSION['pid']) makes me think that you want to select multiple rows by their pid. Try to make use of IN operator:

SELECT id AS wid FROM mywishlist
WHERE pid IN ($pid)
Sign up to request clarification or add additional context in comments.

Comments

2

your query is wrong, use this query

$result=mysql_query("SELECT id AS wid FROM mywishlist where pid IN($pid) ") 

7 Comments

what is difference between above answer and your ?
yogesh i used this query but results are still same only fetch one row?
@FarhanDharsi Run this query in phpmyadmin and check how many rows it returns. And also remove this line $results; after while loop.
yes i tried that query and fetching alll rows from this query ... in phpmyadmin
@FarhanDharsi after while loop use print_r($results); and see that what it contains.
|
1

Try

$results= array();

while($row=mysql_fetch_array($result))
{
    $results[] = $row['wid'];
}

And For Loop as.

$max = count($results);
for($j=0; $j<$max; $j++)
{
?>
    <input type="text" name="wid[]" value="<?php echo $results[$j]; ?>" />
<?php
}
?>

Comments

Your Answer

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