[ Problem ]
Can't seem to get mysql_query to return properly using regular return when returning to a function from a function.
[ Code ]
Made this little script to illustrate how list can successfully return a query that a simple return fails to do.
<?
/*****************************
* Change:
* $return_choice; // either 1 or 2 depending, see comment beside var
* $db_table; // call whatever table you like, long as it exists and has rows it'll do nicely
* 3 variables in function db_connect_users(); // change them appropriately at bottom of script
****************************/
// defines vars
$return_choice = 2; // 1 = PLAIN RETURN 2 = LIST RETURN
$db_table = 'raw_tbl_statistics';
// calls main func
func($return_choice, $db_table);
function func($return_choice, $db_table)
{
if ($return_choice==1) {
return_result_query($db_table, $return_choice);
} // calls plain RETURN that will FAIL
else if ($return_choice==2) {
list ($result_query) = return_result_query($db_table, $return_choice);
} // calls for LIST array return that will work PROPERLY
// gets number of rows and tells whether or not it was successful
$num_results = mysql_num_rows($result_query);
if (!$num_results) {
echo "<BR><B>FAILED to execute properly</B><BR>";
} else {
echo "<BR><B>WORKED properly</B><BR>";
}
};
function return_result_query($db_table, $return_choice)
{
global $result_query;
db_connect_users();
$result_query = mysql_query("SELECT * FROM $db_table");
if ($return_choice==1) {
return $result_query;
}
else if ($return_choice==2) {
return array ($result_query);
}
};
////////////////////////////////////////// DATABASE FUNCTION ////////////////////////////////////////////////////
function db_connect_users()
{
$database = '';
$db_user_name = '';
$db_user_pass = '';
$result = mysql_connect("localhost", "$db_user_name", "$db_user_pass");
if (!$result) {
echo " COULD NOT CONNECT USER: <b>$db_user_name</b><BR>";
return 0;
}
@mysql_select_db($database) or die( "Unable to select database: <b>$database</b>");
};
?>
[ Question ]
Can the return be fixed somehow (am I messing anything up) OR is there some sort of another way OR is using list the only way?