Jump to content

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


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?

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.