Michael Kuan
Michael Kuan

Reputation: 1101

PHP array mysql retrieve each record

Upvotes: 0

Views: 127

Answers (5)

 for($i = 0; $i < count($serialID); $i++)
{
$r = mysqli_query($sql);
$sql=select serial from book where serial like "$serialID[$i]"
}

 while($row = mysqli_fetch_array($r)
 {
 echo $row['serial'].'<br />';
  } 

Upvotes: 0

potashin
potashin

Reputation: 44581

You can use this instead :

$q = "select serial from book where serial like '%"
   . implode($serialID, "%' OR serial LIKE '%") . "%'";
$r = mysqli_query($dbc, $q);

while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)
   echo $row['serial'].'<br />';

In your for loop you SELECT all of the rows, but only the last instance is saved in a variable ( in the last iteration of the for loop), so you fetch only it in the while loop.

P.S. You could also work it out by putting your while loop in the end of the for loop, but the code above is just not that cumbersome.

Upvotes: 1

JulioJaavier
JulioJaavier

Reputation: 91

$serialID = array("0001","0002","0003","0004","0005");

//DB Connection
foreach($serialID as $serial){
    $q = "select serial from book where serial like '%".$serial."%' limit 1";
    $r = mysqli_query($dbc, $q);
    while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)
        echo $row['serial'].'<br/>';
}

Upvotes: 0

Vamshi .goli
Vamshi .goli

Reputation: 520

i hope you have did a mistake that in the query you have kept limit 1 so that's the reason you are getting only last record

for($i = 0; $i < count($serialID); $i++)
{
$sql=select serial from book where serial like \"$serialID[$i]\"
$r = mysqli_query($sql);
}

 while($row = mysqli_fetch_array($r)
 {
 echo $row['serial'].'<br />';
  } 

Upvotes: 0

bish
bish

Reputation: 3419

Edit: you always override your select statement in the for loop before quering it. So only the last query with the last serial is executed

Upvotes: 0

Related Questions