Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return Array from Function? How?

Status
Not open for further replies.

JSProgramIA

Programmer
Oct 28, 2005
41
US
This simple example does not work:

Code:
function getProjectInfo($projectId){

	global $row;

	// Retrieve info for supplied project ID
	// Returns $row
	$page_sql = "SELECT * from myTable";
	
	$result = mysql_query($page_sql);
        $row = mysql_fetch_array($result);
        return $row;

}

and if I call it:

Code:
getProjectInfo("999");

I get nothing ( and I Know there are records ).

Any advice?
 
What is the $projectID parameter supposed to do? You don't use it.

Why are you performing "global" on $row? You should do either a return or just set the value to a global varible, not both.

Where is your database connection handle? It's not global, so you should be using "global" on that variable and explicitly passing it to your mysql_query() invocation.

What are your PHP installation's error outputs set to? Are you getting any errors?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I was wondering about the db connection handle also. Since the default for mysql_query() is to use the last-opened handle, does the handle need to be specified and made global when it's used within a function?

I'd suggest print_r($row) inside the function to see what's there and determine if it's a query problem or a return problem.
 
I was assuimg a lot in my last post. I will be more specific.

1. My connection is included via:

Code:
<? include ("includes/config.php") ?>

Why are you performing "global" on $row? You should do either a return or just set the value to a global varible, not both.

I don't know. I was just trying it.

What is the $projectID parameter supposed to do? You don't use it.

In reality, my SQL is a lot longer and does return rows (as confirmed by Navicat). So for the sake of this example, let's use:

Code:
$page_sql = "SELECT * from myTable where projectid = '$project_id' ";

Lastly, I am not getting any errors.

Could this be something in my $conn string not being global or something? Or do I need to pass the returned row by reference?

Thanks again. Any simple example of returning an array (mysql rows) would be helpful.

Regards.
 
print_r within the function works.

print_r outside of it returns NULL.
 
NEVER MIND I AM AN IDIOT!!!!!!!!

Can you see it?

Code:
function getProjectInfo($projectId){


	// Retrieve info for supplied project ID
	// Returns $row
	$page_sql = "SELECT * , total_hours-hours_paid AS hours_owed,  DATE_FORMAT(last_updated, '%m-%d-%Y') AS last_update FROM  `project`  INNER JOIN `hours` ON (`project`.`project_id` = `hours`.`project_id`) ";
	
	$result = mysql_query($page_sql);
	if(!$result){
	
		die("Error 1");
	
	} else {
	
		$returned_row = mysql_fetch_array($result);
		
		return $row;
		
	}
}


THANKS SO MUCH. I APPRECIATE YOUR HELP.
 
I just tried it and it works fine. And, I've confirmed that the connection handle doesn't need to be specified and made global; omitting it in the function works. I can't see anything in your code to cause the problem (except for the query string, I cut & pasted it to test).

For what it's worth, here's what I tried:

<?php
$conn = mysql_connect("localhost","user","password");
if (!$conn) die;

function getinfo($name)
{
$result = mysql_query("select * from assets.assets where name='$name'");
$row = mysql_fetch_array($result);
return $row;
}

$r = getinfo("JDP01WW0917");
print_r($r);
?>

And the results:
Array
(
[0] => 0917
[tag] => 0917
[1] => JDP01WW0917
[name] => JDP01WW0917
[2] => Server
[type] => Server
...
)

With or without using a global for $row made no difference.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top