0
  • Database (book table)
    serialID   price
     0001     10.00
     0001      30.00
     0002      15.00
     0004(A)  9.00
     0004(B)  5.00
     0005       3.00
    (Noted: 0003 no record)

  • Code

    $serialID = array("0001","0002","0003","0004","0005");
    
    //DB Connection
    for($i = 0; $i < count($serialID); $i++)
    {
        $q = "select * from book where serial like \"$serialID[$i]%\" limit 1";
        $r = mysqli_query($dbc,$q);
    
        while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
        {
              $serial[$i] = $row['serial'];
              $price[$i] = $row['price'];
              echo $serial[$i].' '.$price[$i];
        }
    }
    
    //pass db value into array
    for($j = 0; $j < count($serialID); $j++)
    {
         $data[$j] = array($serialID[$j],$price[$j]); 
    }
    
  • This time my questions is how to skip the serialID 0003 value?

  • My expected output: (echo $serial[$i].' '.$price[$i])
    0001      10.00
    0002      15.00
    0003
    0004(A)   9.00
    0005        3.00

5
  • 3
    Just remove it from your array of codes to search for Commented Jan 13, 2015 at 7:19
  • 4
    is this an IQ test ? Commented Jan 13, 2015 at 7:19
  • @Raptor Did anybody pass? Commented Jan 13, 2015 at 7:20
  • compare with the array and your table records Commented Jan 13, 2015 at 7:21
  • possible duplicate of PHP array mysql retrieve each record Commented Jan 13, 2015 at 7:40

4 Answers 4

0

Modify last loop to

for($j = 0; $j < count($serialID); $j++)
{
if(null != $price[$j]){
     $data[$j] = array($serialID[$j],$price[$j]); 
    }
}

But my question is, it's not sucide to query something in loop? Maybe you should use "IN" statement? Or "OR"? For e.g:

foreach($serialId as $id){
  $string.= ' %'.$serialId. ' % OR '; //of course you should check if this is first or last cell in array. So you don’t add unnecessary OR or space
}
select * from book where serial like \"$string\" limit 1
Sign up to request clarification or add additional context in comments.

1 Comment

I have already try in statement. But not exactly get the value that I want.
0

Just check it in the for loop. Use the code below

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

//DB Connection
for($i = 0; $i < count($serialID); $i++)
{
    $q = "select * from book where serial like \"$serialID[$i]%\" limit 1";
    $r = mysqli_query($dbc,$q);

    while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
    {
          $serial[$i] = $row['serial'];
if($serialID=="0003"){
          $price[$i] = $row['price'];
    }
          echo $serial[$i].' '.$price[$i];
    }
}

//pass db value into array
for($j = 0; $j < count($serialID); $j++)
{
     $data[$j] = array($serialID[$j],$price[$j]); 
}

Hope this helps you

2 Comments

I don't think this will help OP
@Shaiful Islam Updated my answer
0

I think this code should work better for your needs:

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

//DB Connection
foreach ($serialID as $serialItem) {
    $q = "select * from book where serial like \"$serialItem%\" limit 1";
    $r = mysqli_query($dbc, $q);

    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        if (count($row) > 0) {
            echo $row['serial'] . ' ' . $row['price'];

            //pass db value into array
            $data[] = array(
                'serial' => $row['serial'],
                'price'  => $row['price']
            );
        }
    }
}

// debug output
print_r($data);

Comments

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

$preparing = [];
foreach($serialID as $value) {
 $preparing[] = "'" . $value . "'";
}

$sql = "select * from book where serial IN (" . implode(',', $preparing) . ")";
$query = mysqli_query($sql);

$data = mysqli_fetch_all($query, MYSQLI_ASSOC);

Comments

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.