473,385 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with a mysql array step thru in PHP

290 100+
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
2 2243
Dormilich
8,658 Expert Mod 8TB
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
jeddiki
290 100+
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

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

Similar topics

19
by: Westcoast Sheri | last post by:
To keep track of how many fruits my visitors buy, I use a mySQL database (2 columns: "fruit" and "quantity")....so can we make these following mySQL queries work somehow? (visitor buys 5...
0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
0
by: Fatt Shin | last post by:
Hi, I'm running MySQL 4.0.13, connecting from PowerBuilder 9 using ODCB Connector 3.51. I'm facing a problem where whenever I issue a SELECT COUNT(*) statement from PowerBuilder, I always get SQL...
0
by: Jack Lauman | last post by:
I have a table with a column "date" that contains dates in SQL format, i.e. 2003-08-10 I need to query the table and return all the rows from the current date thru the next 6 days. (i.e. today...
6
by: Murtix Van Basten | last post by:
HI all, I am trying to migrate a database from mysql to mssql2k. I use myODBC to connect to mysql server to pull the database from DTS and insert in to sql server. But in mysql server, there is...
14
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
9
by: Ben | last post by:
Hello, I'll bet this has been asked a million times but I can't seem to find a thread that gives the clear example I need. This PC has MySQL and IIS configured and running. The MySQL database is...
2
by: gellis72 | last post by:
I'm working on a program that imports a bunch of data from a folder full of Excel files and compiles it into an Access DB. The Excel files have a varying number of rows and columns that need to be...
5
by: Etrex | last post by:
Hello, This is my first attempt at a c++ program, and it is a long post, please bear with me. I'm trying to read in a text file containing a firewall log, make the information...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.