travis77 Posted March 15, 2007 Share Posted March 15, 2007 Hello, I'm trying to add a column to the results of a query. So, I get the records $r = $db->query($sql) or error ('Critical Error', mysql_error ()); Now I want to add an extra column and populate that column to the $r recordset. Is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/42875-append-column-to-query-results/ Share on other sites More sharing options...
per1os Posted March 15, 2007 Share Posted March 15, 2007 Where is the $sql located, it would have to be placed in that section...To get the extra column you would need to mysql_fetch_assoc($result); mysql_fetch_array($result); Unless that $db->query() function does that for you, that definiation might help out also. Quote Link to comment https://forums.phpfreaks.com/topic/42875-append-column-to-query-results/#findComment-208219 Share on other sites More sharing options...
travis77 Posted March 15, 2007 Author Share Posted March 15, 2007 So are you saying that the query should have the extra field in it - $sql = "Select field1, field2, 0 from tblTest" where 0 is the extra field? Then I would have to use fetch_array to fetch each row and add what I need to that extra field? Quote Link to comment https://forums.phpfreaks.com/topic/42875-append-column-to-query-results/#findComment-208231 Share on other sites More sharing options...
per1os Posted March 15, 2007 Share Posted March 15, 2007 Yep, to use the fetcharray it goes like while ($row = mysql_fetch_array($r)) { print $row['field1']; // processing here. } Quote Link to comment https://forums.phpfreaks.com/topic/42875-append-column-to-query-results/#findComment-208239 Share on other sites More sharing options...
travis77 Posted March 15, 2007 Author Share Posted March 15, 2007 Ok, so after I use fetch_array and put the value in that I want while ($row = mysql_fetch_array($r)) { $row['field1'] = $i; // processing here. $i++; } Is there a way to put that back into $r? Quote Link to comment https://forums.phpfreaks.com/topic/42875-append-column-to-query-results/#findComment-208249 Share on other sites More sharing options...
per1os Posted March 15, 2007 Share Posted March 15, 2007 Not without an updating statement. via SQL. Quote Link to comment https://forums.phpfreaks.com/topic/42875-append-column-to-query-results/#findComment-208251 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.