I've coded a fairly small PHP script that works fine on its own, and I now need to convert it into a Mambo component (for 4.5.1). So far, everything seems to work fine, except for database lookups...
If I run the following code in the script, the Mambo database lookup performs correctly and I get a result:
PHP Code:
$gymboQuery = "SELECT name FROM mos_users WHERE id=$userID";
$database->setQuery($gymboQuery);
$user = mysql_fetch_assoc($database->query());
echo "<p>Thanks <strong>".$user['name']."</strong>. Your User ID is <strong>".$userID."</strong>.</p>";
But if I put that code into a function and call that separately, nothing happens.
PHP Code:
echo "<p>Thanks <strong>".getUsername($userID)."</strong>. Your User ID is <strong>".$userID."</strong>.</p>";
where getUsername is
PHP Code:
function getUsername($uid){
global $database;
$gymboQuery = "SELECT name FROM mos_users WHERE id=$uid";
$database->setQuery($gymboQuery);
$user = mysql_fetch_assoc($database->query());
return $user['name'];
}
Practically identical code... but nothing is passed back to the first script. What am I missing?
There are about half a dozen such database lookups in various functions, none of which work. But when they're run directly from the script (not from the function) they work fine. The functions work fine when I'm not using the Mambo database global, so I assume something's going wrong there...