user3438096 user3438096 - 6 months ago 8
PHP Question

Multiple array query in one mysql_num_rows in MySQL and PHP

I want the result for this array sql query in one mysql_num_rows
how can I do that? i always get the result for last array query

I have MySQL records example: "1290511", "211223","08910"

$no=array("905","1122","891");
$arrlength=count($no);

for ($x=0; $x<$arrlength; $x++) {
$result = mysql_query(
"SELECT * FROM spe_no WHERE replace(sp_no,' ','') LIKE '%{$cars[$x]}%'"
) or die(mysql_error());
}

while ($row = mysql_fetch_array( $result )) {
$number = $row['sp_numbers'];
echo $number;
}


i always get the last record with "891"
it should all the example

Please help

Answer

You are issuing a query multiple times, but only fetching the result set from the last time you issue it. I believe you want this.

for($x=0;$x<$arrlength;$x++) {
  $result = mysql_query("SELECT * FROM spe_no WHERE replace(sp_no,' ','') LIKE '%{$cars[$x]}%'") or    die(mysql_error());

  while($row = mysql_fetch_array( $result )) {
    $number = $row['sp_numbers'];
    echo $number;
  } 
}

Pro tip: Always indent your conditionals and loops so you can see at a glance the logic of your program's flow.

Pro tip: Avoid the mysql_interface, and switch to mysqli or PDO::.

Pro tip: Avoid SELECT *. Instead use a list of columns such as SELECT sp_numbers.

Comments