Jump to content

[SOLVED] Can you put retrieved data from a while statement into a variable?


SocomNegotiator

Recommended Posts

Ok my question is if I had a while statement that was grabbing more than row of information from the database is there a way that I can put those results into a variable or an array?

 

<?php
$res = mysql_query("SELECT * FROM `order` WHERE user_id = '$userid'") or die(mysql_error());
while($r = mysql_fetch_array($res)){
Some database info and what not
}

//Can I use a variable as the results to the while statement? So...
$results = the results from the while statement
$results[] = the results from the while statement

//Which way would work if any could work?
echo '$results';
echo '$results[]';
?>

 

 

<?php
while(condition = true)
{
   //preform query here
  $data = 'query result';
}
[code]
Nothing specail.
Variables defined in while loops are local outside to loop so you can assign it like normal then use it like normal.

[/code]

Now this would work if I had 4 different items to grab...? Like a few lines below all of that could I just use $data and it will display all the items that were brought up from the while

 

Well what I am trying is to send an email to myself with a list of items that someone has purchased in one order. I can get all the items in the while statement, but to be able to send them in the email I am not sure about...and that is why I would need it to be a variable or an array.

To populate an array, and OOP...

 

<?php

$results = array( );
$res = mysql_query( 'SELECT * FROM `order` WHERE `user_id` = \'$userid\'' ) or die( mysql_error( ) );
while ( $r = mysql_fetch_object( $res ) ) {
array_push( $results, $r->field );
}
print_r( $results );

?>

I tried that and I printed it to the page instead of trying the email first and on the page it just said Array()

 

 

 

To populate an array, and OOP...

 

<?php

$results = array( );
$res = mysql_query( 'SELECT * FROM `order` WHERE `user_id` = \'$userid\'' ) or die( mysql_error( ) );
while ( $r = mysql_fetch_object( $res ) ) {
array_push( $results, $r->field );
}
print_r( $results );

?>

Do I put that inside of the while statement? Or are you saying just use that code period...? If I only use that code it will only allow me to use one row in the database if you mean just like this $row['item_name']. I need the while statement so it will grab every row.

 

just do it like this....

 

<?php
$res = mysql_query("SELECT * FROM `order` WHERE user_id = '$userid'") or die(mysql_error());
$row = mysql_fetch_array($res);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.