Jump to content

[SOLVED] Question :: Getting results from a function?


Solarpitch

Recommended Posts

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;


}

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

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?

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.