I need a simple example of loading arrays from query results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

I need a simple example of loading arrays from query results

Post by steedvlx »

Hello all,

Would someone mind posting a an example of how to do this right? My sample code is below, but for some reason (or many reasons) I cannot get the data from the query into my array.

In case I have totally screwed up the code, here is what I need to do...

I want to loop through a record set with an undetermined number of records. First determine how many rows there are and for each record, I want to assign some of each rows values to array values using an index which is defined by the ordinal number of the current record.

This array values must be preserved in memory until the page into which the script is 'included' is dismissed.

Thanks for any help. This is a new area for me.

------------------------------------
SteedVLX


Code: Select all

require_once('../Connections/kb_conn.php'); $colname_rs_load_order = "1";
if (isset($HTTP_SESSION_VARS['session_usernumber'])) {
  $colname_rs_load_order = (get_magic_quotes_gpc()) ? $HTTP_SESSION_VARS['session_usernumber'] : addslashes($HTTP_SESSION_VARS['session_usernumber']);
}
mysql_select_db($database_kb_conn, $kb_conn);
$query_rs_load_order = sprintf("SELECT * FROM cart WHERE usernumber = %s", $colname_rs_load_order);
$rs_load_order = mysql_query($query_rs_load_order, $kb_conn) or die(mysql_error());
$row_rs_load_order = mysql_fetch_assoc($rs_load_order);
$totalRows_rs_load_order = mysql_num_rows($rs_load_order);

	$total_rows_plus_one = $totalRows_rs_load_order + 1
	$counter = 1
do {
		$usernumber[$counter] = $session_usernumber;
		$userid[$counter] = $row_rs_load_order['user_id'];
		$line_num[$counter] = $counter;
		$part_num[$counter] = $row_rs_load_order['cart_kb_prod_part_num'];
		$e_desc[$counter] = $row_rs_load_order['prod_e_desc'];
		$j_desc[$counter] = $row_rs_load_order['prod_j_desc'];
		$line_price[$counter] = $row_rs_load_order['cart_prod_sell_price_jpy'];
		$quantity[$counter] = $row_rs_load_order['cart_quantity'];
		$line_total[$counter] = $row_rs_load_order['cart_line_total'];
		$cart_id_num[$counter] = $row_rs_load_order['cart_id_num'];
		$prod_id_index[$counter] = $row_rs_load_order['cart_prod_id_index'];
		$counter = $counter + 1;
} while ($counter < $total_rows_plus_one);

mysql_free_result($rs_load_order);
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$row_rs_load_order = mysql_fetch_assoc($rs_load_order);
You're doing this only once, fetching one record. You have to do it within the loop.

Are you sure you need to keep the whole resultset in memory?
And if (ok, might be ;-) ) wouldn't it be easier to store the records as is? e.g.

Code: Select all

<?php
mysql_select_db($database_kb_conn, $kb_conn) or die(mysql_error());
$query_rs_load_order = 'SELECT * FROM cart WHERE usernumber='.(int)$colname_rs_load_order;
$rs_load_order = mysql_query($query_rs_load_order, $kb_conn) or die(mysql_error());

while($row = mysql_fetch_assoc($rs_load_order))
{
	$row['usernumber'] = $session_usernumber;
	$recordset[] = $row;
}

// <- lots of php code here -> \\

foreach($recordsets as $record)
	echo $record['cart_quantity'], '<br />';

// the record "in the middle"
$index = count($recordset) / 2;
echo $recordset[$index]['prod_j_desc'];
?>
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

Volka,

Umm. Thanks for the spanking. :wink:

I think I'm not quite ready for some of the complications that code example would bring me. There are at least 4 elements I've never seen before.

Right now, I'm trying to understand the simple, straightforward (read -beginners) approach to looping through a conditional resultset and storing bits of each record into arrays which can be accessed by another script.

The more complicated stuff I'm just not ready for yet.

Thanks.
Post Reply