Forum Moderators: coopster

Message Too Old, No Replies

mysql_fetch_array

mysql_fetch_array problem

         

jeffgman

7:08 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



What am I doing wrong with this code:

foreach($order_qty as $id_no => $val) {

$detailqry2 = "SELECT * FROM misc_order_detail, misc_order_items WHERE misc_order_detail.order_no = '".$order_no."' AND misc_order_items.id_no = '".$id_no."'";

$result2=mysql_query($detailqry2, $conn);

while ($row=mysql_fetch_array($result2, $conn)) {
$description=$row["description"];
$vpc=$row["vpc"];
$min=$row["min"];
$max=$row["max"];
$order_qty2=$row["order_qty"];

$message .= '
<TR>
<TD>".$description."</TD>
<TD>".$vpc."</TD>
<TD><center>".min."</center></TD>
<TD><center>".$max."</center></TD>
<TD><center>".$package_qty."</center></TD>
<TD><center>".$order_qty2."</center></TD>
</TR>
';
}
}

The output just looks like this:

Description
VPC
Min
Max
Pkge Qty
Order

".$description."
".$vpc."
".min."
".$max."
".$package_qty."
".$order_qty2."

".$description."
".$vpc."
".min."
".$max."
".$package_qty."
".$order_qty2."

Thanks for the help.

timster

7:14 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's your problem:

$message .= '

Variables are not interpolated inside single quotes. Either use double quotes and escape the literal quotes, or concatenate. What I mean is:

$html = "<td class=\"something\">$variable</td>"

or

$html = '<td class="something">' . $variable . '</td>'

PhraSEOlogy

7:14 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



Try using just a single quote around the variables.

WHERE misc_order_detail.order_no = '.$order_no.' AND misc_order_items.id_no = '.$id_no.'";

theriddla1019

7:18 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



This is how i would do it...

<?
foreach($order_qty as $id_no => $val) {
$detailqry2 = "SELECT * FROM misc_order_detail, misc_order_items WHERE misc_order_detail.order_no = '".$order_no."' AND misc_order_items.id_no = '".$id_no."'";

$result2=mysql_query($detailqry2, $conn);

while ($row=mysql_fetch_array($result2, $conn)) {
$description=$row['description'];
$vpc=$row['vpc'];
$min=$row['min'];
$max=$row['max'];
$order_qty2=$row['order_qty'];
?>

<TR>
<TD><?=$description?></TD>
<TD><?=$vpc?></TD>
<TD><center><?=min?></center></TD>
<TD><center><?=$max?></center></TD>
<TD><center><?=$package_qty?></center></TD>
<TD><center><?=$order_qty2?></center></TD>
</TR>

<?
}
}
?>

theriddla1019

7:22 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



PhraSEOlogy he has to break out of the statement(im pretty sure) to input a php variable into a sql statment. (But then again i learn something new everyday)

PhraSEOlogy

7:27 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



I use placeholders in my own code so I dont have to worry about quoting.

jeffgman

7:29 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



Thank you very much for the help. It was the quoting. I was not aware of the single/double quote and variables. But, now I have another problem.

I have three databases set up. The first one, called misc_order_items, is a list of items available to order via a web page. The web page goes through each item in the database and lists them out in a table. The order column is set up as an array. Once they hit submit, the header data (store number, date, and order number) is stored in a database I call misc_order_header. Once that INSERT takes place, I have a foreach statement and I insert each line of the order, currently there are 16 items, into a detail database. That database is called misc_order_detail. The detail line has the same $id_no as the items database to make sure that I can always refer back to the original item.

In the script above, after I have inserted all of the data into the three databases I am trying to send out an e-mail with the data. That is where this script comes into play, but I am not doing the select or fetch correctly. Here is the complete mail script:

// send the mail

// recipients

$to = "Jeff Grossman <****@****.com>";

// subject

$subject = "Weekly Store Order";

// from
// from data snipped!

// message

$message = "
<HTML>
<HEAD>
<TITLE>Weekly Store Order</TITLE>
</HEAD>
<BODY>
Weekly Store Order - Store: {$store}<BR><BR>
<TABLE>
<TR>
<TD width=\"250\"><center><b><u>Description</u></b></center></TD>
<TD width=\"60\"><center><b><u>VPC</u></b></center></TD>
<TD width=\"60\"><center><b><u>Min</u></b></center></TD>
<TD width=\"60\"><center><b><u>Max</u></b></center></TD>
<TD width=\"80\"><center><b><u>Pkge Qty</u></b></center></TD>
<TD width=\"60\"><center><b><u>Order</u></b></center></TD>
</TR>
";
foreach($order_qty as $id_no => $val) {

$detailqry2 = "SELECT * FROM misc_order_detail, misc_order_items WHERE misc_order_detail.order_no = '".$order_no."' AND misc_order_items.id_no = '".$id_no."'";

$result2=mysql_query($detailqry2, $conn);

while ($row=mysql_fetch_array($result2, $conn)) {
$description=$row["description"];
$vpc=$row["vpc"];
$min=$row["min"];
$max=$row["max"];
$order_qty2=$row["order_qty"];

$message .= "
<TR>
<TD>{$description}</TD>
<TD>{$vpc}</TD>
<TD><center>{$min}</center></TD>
<TD><center>{$max}</center></TD>
<TD><center>{$package_qty}</center></TD>
<TD><center>{$order_qty2}</center></TD>
</TR>
";
}
}

$message .= "
<TR>
<TD>&nbsp;</TD>
</TR>
<TR>
<TD colspan=\"6\"><textarea rows=\"5\" cols=\"80\" name=\"comment\" wrap>{$comment}</textarea></TD>
</TR>
</TABLE>
</BODY>
</HTML>
";

//To send HTML mail, you can set the Content-type header.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

//additional headers
$headers .= "To: {$to}\r\n";
$headers .= "From: {$from}\r\n";

//and now mail it
mail($to, $subject, $message, $headers);

The correct data is in the database, I can't seem to get it out correctly. Here is the output from the mail command:

Weekly Store Order - Store: 09

Description
VPC
Min
Max
Pkge Qty
Order

4473's
{min}
2
9

4473's
{min}
2
8

<same repeated many times>

Bank Deposit Bags
{min}
100
9

Bank Deposit Bags
{min}
100
8

<same repeated many times>

I snipped it. But, it appears to be listing the same item 16 times, instead of each item just once.

Thanks again for your help.

[edited by: jatar_k at 7:58 pm (utc) on Aug. 5, 2004]
[edit reason] snipped repetitive code [/edit]

theriddla1019

7:34 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



have you tried to select unique?
do you need the for each?

jeffgman

8:31 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



Okay, the for each was the problem. At least now it only prints out 16 lines, which is what is in the misc_order_items database. But, it is printing the same description out for each line. The last description in the items database. Let me try and explain what I want. I am sure it is the SELECT statement which is incorrect now.

I want to cycle through the detail database and find each occurance of the same order_no. Then go through those items and find the respective id_no in the items database that goes with that particular line in the detail database.

Is my SELECT statement incorrect?

jeffgman

9:02 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



Okay, I finally got it to work. Here is the code I ended up using. Does it look okay, or is there a way I can streamline some of the code?

$detailqry2 = "SELECT * FROM misc_order_detail WHERE order_no = '".$order_no."'";

$result2=mysql_query($detailqry2, $conn);

while ($row=mysql_fetch_array($result2, $conn)) {
$order_no2=$row["order_no"];
$id_no=$row["id_no"];
$sku=$row["sku"];
$order_qty2=$row["order_qty"];
$order_date=$row["order_date"];
$store2=$row["store"];

$detailqry3 = "SELECT * FROM misc_order_items WHERE id_no = '".$id_no."'";

$result3=mysql_query($detailqry3, $conn);

while ($row=mysql_fetch_array($result3, $conn)) {
$id_no=$row["id_no"];
$sku=$row["sku"];
$description=$row["description"];
$vpc=$row["vpc"];
$package_qty=$row["package_qty"];
$location=$row["location"];
$min=$row["min"];
$max=$row["max"];

$message .= "
<TR>
<TD>{$description}</TD>
<TD>{$vpc}</TD>
<TD><center>{$min}</center></TD>
<TD><center>{$max}</center></TD>
<TD><center>{$package_qty}</center></TD>
<TD><center>{$order_qty2}</center></TD>
</TR>
";
}
mysql_free_result($result3);
}
mysql_free_result($result2);

theriddla1019

9:47 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



looks good, grats :)

jeffgman

3:46 pm on Aug 6, 2004 (gmt 0)

10+ Year Member



Thank you.