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[]';
?>

 

 

Link to comment
Share on other sites

<?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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 );

?>

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.