Forum Moderators: coopster
function UserDetails() {
global $USERID, $USER, $AID;
$USER = $_SESSION[SESSIONUSER];
$getUserDetails = mysql_query("SELECT * FROM `users` WHERE `username` = '$USER'");
while ($userArray = mysql_fetch_assoc($getUserDetails)) {
$USERID = $userArray['id'];
$AID = $userArray['address_id'];
}
}
function MainAddressDetails() {
global $POSTCODE, $noAddressFound;
if ([b]$AID[/b] > 0) {
$getAddressDetails = mysql_query("SELECT * FROM `address` WHERE `id` = '$AID'");
$numberOfAddresses = mysql_num_rows($getAddressDetails);
if ($numberOfAddresses > 0) {
while ($addressArray = mysql_fetch_assoc($getAddressDetails)) {
$POSTCODE = $addressArray['postcode'];
}
} else {
$noAddressFound = "Uh-Oh!";
}
}
}
<?php
UserDetails();
MainAddressDetails();
echo "<p>User: " . $USER . "</p>";
echo "<p>Postcode: " . $POSTCODE . "</p>";
?>
function UserDetails() {
global $USERID, $USER, $AID;
$USER = $_SESSION[SESSIONUSER];
$getUserDetails = mysql_query("SELECT * FROM `users` WHERE `username` = '$USER'");
while ($userArray = mysql_fetch_assoc($getUserDetails)) {
$USERID = $userArray['id'];
$AID = $userArray['address_id'];
}
}
function MainAddressDetails() {
global $POSTCODE, $noAddressFound, $AID;
if ($AID > 0) {
$getAddressDetails = mysql_query("SELECT * FROM `address` WHERE `id` = '$AID'");
$numberOfAddresses = mysql_num_rows($getAddressDetails);
if ($numberOfAddresses > 0) {
while ($addressArray = mysql_fetch_assoc($getAddressDetails)) {
$POSTCODE = $addressArray['postcode'];
}
} else {
$noAddressFound = "Uh-Oh!";
}
}
}
<?php
include 'functions.php';
UserDetails();
MainAddressDetails();
echo "<p>User: " . $USER . "</p>";
echo "<p>Postcode: " . $POSTCODE . "</p>";
?>
Which do you think is faster:
function UserDetails($user) {
// initialize
$userid=$aid=null;
if (! $user) { die("no user passed to user details"); }
// Actually, the above should be changed to validate the $user value too . . .
$getUserDetails = mysql_query("SELECT * FROM `users` WHERE `username` = '$user'");
// only one record - use IF, not WHILE
if ($userArray = mysql_fetch_assoc($getUserDetails)) {
$userid = $userArray['id'];
$aid = $userArray['address_id'];
}
return Array($userid,$aid);
}
//
function MainAddressDetails($aid) {
$postcode=$noAddressFound=null;
if (is_numeric($aid) and ($aid >0)) {
// so . . .no need for > 0 block now . . . and
// NO NEED TO COUNT. :-) for a single record, fieldname is more
// efficient than * Again, use if for one record.
$getAddressDetails = mysql_query("SELECT postcode FROM `address` WHERE `id` = '$aid'");
if ($addressArray = mysql_fetch_assoc($getAddressDetails)) {
$postcode = $addressArray['postcode'];
}
else { $noAddressFound = "Uh-Oh!"; }
}
else { die("No aid passed to address details"); }
return Array($postcode,$noAddressFound);
}
<?php
include($_SERVER['DOCUMENT_ROOT']."/functions.php");
// See above, validate this too, not just check if set
if (isset($_SESSION[SESSIONUSER])) {
list($USERID,$AID) = UserDetails($_SESSION[SESSIONUSER]);
// since $aid is initialized as null, in UserDetails, we don't need to
// check if isset - simple if is all that's required
if ($AID) {
list($POSTCODE,$MAILERROR) = MainAddressDetails($AID);
if ($MAILERROR) { echo $MAILERROR; }
else {
echo "<p>User: $USER </p>";
echo "<p>Postcode: $POSTCODE </p>";
}
}
else { die("Session user OK, no AID"); }
}
else { die("No session user has been set"); }
//
// OK let's look up another user!
// Of course, validate this input - for example only
//
if (isset($_GET['other_user'])) {
// Note we can use any variables in this scope.
list($newuserid,$newaid) = UserDetails($_GET['other_user']);
if ($newaid) {
list($newcode,$newerr) = MainAddressDetails($newaid);
if ($newerr) { echo $newerr; }
else {
echo "<p>New User: $newuserid </p>";
echo "<p>New Postcode: $newcode </p>";
}
}
else { die("New user input OK, no AID"); }
}
?>
[edited by: Matthew1980 at 9:12 pm (utc) on Jun 18, 2010]