Solarpitch Posted September 29, 2007 Share Posted September 29, 2007 Hi, I have the below function that will return an array of results as its return value. Basically I just want to know how I retrieve each result in the array and print to screen. I thought it was something like echo $row[1], $row[2] ...etc but that doesnt seem to work. Thanks function edituserad($adid) { $sql = "select * from test_ads2 where ad_id=".$adid.""; $result = mysql_query($sql); while(($row = mysql_fetch_row($result)) != false) { $row2 = $row; } return $row2; } Link to comment https://forums.phpfreaks.com/topic/71173-solved-question-getting-results-from-a-function/ Share on other sites More sharing options...
rarebit Posted September 29, 2007 Share Posted September 29, 2007 function edituserad($adid) { $row2 = array(); $sql = "select * from test_ads2 where ad_id=".$adid.""; $result = mysql_query($sql); while(($row = mysql_fetch_row($result)) != false) { $row2[] = $row; } return $row2; } Link to comment https://forums.phpfreaks.com/topic/71173-solved-question-getting-results-from-a-function/#findComment-357992 Share on other sites More sharing options...
Solarpitch Posted September 29, 2007 Author Share Posted September 29, 2007 Cheers, I am trying to call the result of that like... $result = edituserad($adid); echo $result[1]; echo $result[2]; That isn't correct.. is it! Link to comment https://forums.phpfreaks.com/topic/71173-solved-question-getting-results-from-a-function/#findComment-357994 Share on other sites More sharing options...
rarebit Posted September 29, 2007 Share Posted September 29, 2007 It would start at 0. You might want to use a foreach: foreach($result as $e) { echo $e; } You could also count the number first to make sure you've got some results? Also you could make your function a bit more oop like this: function do_query($sql) { $ret = array(); try { if ( !@ ($result = mysql_query($s, $conn)) ) throw new Exception (mysql_error()); } catch (Exception $e) { //echo 'ERROR: ' . $e->getMessage(); return -1; } while(($row = mysql_fetch_row($result)) != false) { $ret[] = $row; } return $ret; } $adid = "select * from test_ads2 where ad_id=999"; $result = do_query($adid); if($result != -1) { foreach($result as $e) { echo $e; } } Link to comment https://forums.phpfreaks.com/topic/71173-solved-question-getting-results-from-a-function/#findComment-358005 Share on other sites More sharing options...
Solarpitch Posted September 29, 2007 Author Share Posted September 29, 2007 Hey, I almost have this working. I prints all the values of the array fine. But now I would like to assign all the values in the array to a variable. I am currently trying... foreach($result as $e) { $id = $e[0]; echo $id; } This seems to out put the address in memory rather than the actual result value. How can I modify that to assign the DB value? Link to comment https://forums.phpfreaks.com/topic/71173-solved-question-getting-results-from-a-function/#findComment-358098 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.