SocomNegotiator Posted July 24, 2008 Share Posted July 24, 2008 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 https://forums.phpfreaks.com/topic/116363-solved-can-you-put-retrieved-data-from-a-while-statement-into-a-variable/ Share on other sites More sharing options...
unkwntech Posted July 24, 2008 Share Posted July 24, 2008 <?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 https://forums.phpfreaks.com/topic/116363-solved-can-you-put-retrieved-data-from-a-while-statement-into-a-variable/#findComment-598344 Share on other sites More sharing options...
SocomNegotiator Posted July 24, 2008 Author Share Posted July 24, 2008 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 https://forums.phpfreaks.com/topic/116363-solved-can-you-put-retrieved-data-from-a-while-statement-into-a-variable/#findComment-598346 Share on other sites More sharing options...
Third_Degree Posted July 24, 2008 Share Posted July 24, 2008 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 https://forums.phpfreaks.com/topic/116363-solved-can-you-put-retrieved-data-from-a-while-statement-into-a-variable/#findComment-598348 Share on other sites More sharing options...
SocomNegotiator Posted July 24, 2008 Author Share Posted July 24, 2008 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 https://forums.phpfreaks.com/topic/116363-solved-can-you-put-retrieved-data-from-a-while-statement-into-a-variable/#findComment-598352 Share on other sites More sharing options...
ronnie88 Posted July 24, 2008 Share Posted July 24, 2008 u include your db info into the script?? Link to comment https://forums.phpfreaks.com/topic/116363-solved-can-you-put-retrieved-data-from-a-while-statement-into-a-variable/#findComment-598360 Share on other sites More sharing options...
MasterACE14 Posted July 24, 2008 Share Posted July 24, 2008 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 https://forums.phpfreaks.com/topic/116363-solved-can-you-put-retrieved-data-from-a-while-statement-into-a-variable/#findComment-598398 Share on other sites More sharing options...
SocomNegotiator Posted July 24, 2008 Author Share Posted July 24, 2008 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 https://forums.phpfreaks.com/topic/116363-solved-can-you-put-retrieved-data-from-a-while-statement-into-a-variable/#findComment-598770 Share on other sites More sharing options...
SocomNegotiator Posted July 24, 2008 Author Share Posted July 24, 2008 Ah I found a way that would work....and it is pretty much unkwntech said, thanks a lot for your help guys! Link to comment https://forums.phpfreaks.com/topic/116363-solved-can-you-put-retrieved-data-from-a-while-statement-into-a-variable/#findComment-598844 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.