jocast Posted March 28, 2011 Share Posted March 28, 2011 Hello, this is my first post in this forum, in fact my first post about php. I am developing a crud application, but when i am trying to list a set of results it gives me more that the expected Let me explain. if i print_r my array (which i get it from a sql result) it gives me this Array ( [0] => SHR1 [codprod] => SHR1 [1] => REGULAR SERVICE HOUR [proddescription] => REGULAR SERVICE HOUR [2] => 3.00 [qty] => 3.00 [3] => HR => HR [4] => 45.0000 [price] => 45.0000 [5] => 0.00 [discount] => 0.00 [6] => 135.00 [amount] => 135.00 ) if I foreach($detailrow as $value){ echo $detailrow['codprod'] . ", " . $detailrow['proddescription'] . ", " . $detailrow['qty'] . ", " . $detailrow['code'] . ", " . $detailrow['price'] . ", " . $detailrow['discount'] . ", " . $detailrow['amount'] . "<br/>"; } It gives me 14 rows with the same info. I run the query in mysql and it gives me only 1 result as expected. I noticed that the 14 rows are the same quantity of items in the array not results but let say columns, i dont know if it has something to do with the results. My questions: Why it is giving me 14 rows instead just one? What is the $value in the foreach function? why can't i just say foreach($myarray)? why the print_r shows me the items array duplicated using the array position and the item name? Thank you for your help!! Link to comment https://forums.phpfreaks.com/topic/231920-foreach-giving-more-rows-han-expected/ Share on other sites More sharing options...
requinix Posted March 28, 2011 Share Posted March 28, 2011 Ha. I saw the thread title, made a guess, and was totally right 1. mysql_fetch_array will give you two values for every column in the query. One value has a number (an offset 0->6) as a key, one has a string (a column name) as a key. Seven columns in the result means 14 items in the array. 2. You're foreaching on the array when you don't need to. Shouldn't, even. What you want is an array. What you have is an array. You don't have to do anything else. Link to comment https://forums.phpfreaks.com/topic/231920-foreach-giving-more-rows-han-expected/#findComment-1193150 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.