Forum Moderators: coopster

Message Too Old, No Replies

Query in a For Loop always found only the 1st query

         

Gian04

1:26 pm on Nov 3, 2008 (gmt 0)

10+ Year Member



Table :
fruits
(2 fields - fruitname & description)


fruitname ¦ description
banana ¦ banana description here
mango ¦ mango description here
apple ¦ apply description here
orange ¦ orange description here
pineapple ¦ pineapple description here
guava ¦ guava description here
melon ¦ melon description here

$mystring = banana, mango, apple, pineapple, orange
$show_all_fruits = explode(",",$mystring );

for($i=0; $i < sizeof($show_all_fruits); $i++) {
$query_fruit = "SELECT * FROM fruits WHERE fruitname = '".$show_all_fruits[$i]."'";
$fruit_result = mysql_query($query_fruit) or die(mysql_error());

if (mysql_num_rows($fruit_result) > 0) {
$row_fruit = mysql_fetch_row($fruit_result);
echo $row_fruit[1];
}
}

Im sure that all the listed fruit in

$mystring
exist in the table but it always found only 1 record - the first one (banana).

Further test shows that after executing the first query it will return 0 record for the succeeding query in the loop even though the fruit name exist in the table

Whats wrong with my query / for loop? Thanks

omoutop

1:35 pm on Nov 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Bold parts shows my editing

$mystring = "banana, mango, apple, pineapple, orange"

SELECT fruitname FROM fruits WHERE fruitname = '".$show_all_fruits[$i]."'

echo $row_fruit[0];

Thy the above and see what happens

Gian04

2:21 pm on Nov 3, 2008 (gmt 0)

10+ Year Member



Hi omoutop,

Sorry I just forgot to place " " but my actual code have it.

Following your recommendation did not solve the problem, any other solution please help, Im stuck with it.

Gian04

2:35 pm on Nov 3, 2008 (gmt 0)

10+ Year Member



Well I just found the problem. It is just the extra space after the comma of each fruit