By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,724 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,724 IT Pros & Developers. It's quick & easy.

Problem with a mysql array step thru in PHP

100+
P: 290
Hi

I have two tables and I am trying to step through them
using mysql arrays.

I think that maybe I have not understood the foreach loop
properly. I have two of them, it looks logical to me but
it is throwing up a strange result :confused:

This is the code I have:

Expand|Select|Wrap|Line Numbers
  1. sql_prod = "SELECT * FROM products WHERE owner_id =  '{$_SESSION['member']}' ";
  2. $result_prod = mysql_query($sql_prod)    or die("could not execute FIND MEMBER query.". mysql_error());
  3.  
  4. if(mysql_num_rows($result_prod) == 0 ){
  5.    echo "<td>You have no products recorded!</td><td>0</td><td>0</td><td>0</td>";
  6.    } // end if
  7.  
  8. else {             
  9.         $row_prod = mysql_fetch_assoc($result_prod);
  10.  
  11. echo "<br>Member: {$_SESSION['member']}<br>";
  12. print_r($row_prod);
  13.  
  14.     foreach ($row_prod as $value_prod) {
  15.  
  16.           $sql_req = "SELECT * FROM requests WHERE prod_id =  '{$value_prod['prod_id']}' ";
  17.                 $result_req = mysql_query($sql_req)    or die("could not execute find REQUEST 
  18.  
  19. query.". mysql_error());
  20.  
  21.             $num_req = mysql_num_rows($result_req);
  22.                 if(mysql_num_rows($result_req) == 0 ){
  23.            echo "<td>{$row_prod['prod_name']}</td><td>0</td><td>0</td><td>0</td>";
  24.                    } // end if
  25.             else {             
  26.                     $row_req = mysql_fetch_assoc($result_req);
  27.                      $req_num = 0;
  28.                      $rep_num = 0;
  29.                      $unres = 0;
  30.                      foreach ($row_req as $value_req) {
  31.                           $req_num = $req_num+1;
  32.                                 $rep_num = $rep_num+1;
  33.                           if ($value_req['request'] != "" && trim($value_req['reply']) == "") {
  34.                                           $unres = $unres+1;
  35.                                          } // end if
  36.                                 } // end foreach
  37.                         echo 
  38.  
  39. "<td>{$row_prod['prod_name']}</td><td>$req_num</td><td>$rep_num</td><td>$unres</td>";            
  40.                   } // end else
  41.                echo "</tr><tr>";        
  42.                 } // end foreach
  43.         echo "</tr></table>";
  44.         } // end else
  45.  
AND this is the result I get:

Member: sai612

Array ( [prod_id] => bmm001 [live] => y [type] => L [cat_cd] => [owner_id] => sai612 [prod_name] => 30 Day BM Blitz [request] => 0 )

Notice: Uninitialized string offset: 0 in /home/focus7/public_html/mem_start1.php on line 77

Product Requests Replies Unresolved
30 Day BM Blitz 0 0 0
30 Day BM Blitz 0 0 0
30 Day BM Blitz 0 0 0
30 Day BM Blitz 0 0 0
30 Day BM Blitz 0 0 0
30 Day BM Blitz 0 0 0
30 Day BM Blitz 0 0 0
As you can see it is working a bit !

But I don't know why I get that error, or what it means.

Also the result is odd as I only have ONE row in the "requests" table for
that product.

Can anyone see where I have gone wrong ?

Thanks :)
Aug 19 '09 #1
Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
the foreach loop is not appropriate in this situation. you have to loop over mysql_fetch_assoc() not over one result row.

Expand|Select|Wrap|Line Numbers
  1. while ( $res = mysql_fetch_assoc() )
  2. {
  3.   // do second request
  4. }
there are 2 more ways to deal with that situation, a combined SQL query (JOIN or something like that, though you probably have to ask in the SQL forum for that) and a database abstraction layer (like PDO *gg*) which allows you to fetch all data at once, so that you can loop over the complete result array.
Aug 19 '09 #2

100+
P: 290
Thanks for helping.

I changed the "foreach"
over to :

while ($row_req = mysql_fetch_assoc($result_req)) {
and it works fine now
Aug 19 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.