Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
dpayton
Forum Newbie
Posts: 10 Joined: Fri Jun 24, 2005 1:48 pm
Post
by dpayton » Wed Jun 14, 2006 10:36 am
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I have a query that I have to run over and over in my program. I would like to just set it up as a php function and call it when I need it, but I cannot get it to return the results. My code is posted below.Code: Select all
function getaddress($testsiteid) {
//test for address
$query90 = "select * from expandedaddressjoin where siteid = '$testsiteid'";
$result90 = mysql_query($query90) or die(mysql_error());
if($row90 = mysql_fetch_array($result90)){
$testaddressid = $row90[0];
$testaddressnote1 = $row90[2];
$query91 = "select * from address where addressid = '$testaddressid'";
$result91 = mysql_query($query91) or die(mysql_error());
if($row91 = mysql_fetch_array($result91)){
$teststreetid = $row91[1];
$teststreetnumber = $row91[2];
$testaddressnote2 = $row91[3];
$query92 = "select * from street where streetid = '$teststreetid'";
$result92 = mysql_query($query92) or die(mysql_error());
if($row92 = mysql_fetch_array($result92)){
$teststreetdirection = $row92[1];
return $teststreetdirection
return $teststreetname = $row92[2];
$teststreettype = $row92[3];
return $teststreettype;
$addresscomplete = 'Yes';
}
}
}
getaddress(1);
print "<p>$teststreetnumber $teststreetname $teststreettype</p>
Thanks inadvance!
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Jun 14, 2006 4:10 pm
You're written your code to return three times.. of which one the first (encountered) would ever actually do anything (everything after that is ignored.) Second problem: you're not storing the return value(s) into anything from the function.
Here's an example of how it works:
Code: Select all
$returnValue = functionName($someArgument1, $someArgument2, $someArgumentN);A function only has one return value. This can be any type PHP supports, including an array.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Wed Jun 14, 2006 5:09 pm
Maybe implement it using the Active Record pattern:
Code: Select all
class GetAddress {
var $testsiteid;
var $testaddressid = '';
var $testaddressnote1 = '';
var $teststreetdirection = '';
var $teststreetnumber = '';
var $teststreetname = '';
var $teststreettype = '';
function find($testsiteid) {
$this->testsiteid = $testsiteid;
//test for address
$query90 = "select * from expandedaddressjoin where siteid = '{$this->testsiteid}'";
$result90 = mysql_query($query90) or die(mysql_error());
if($row90 = mysql_fetch_array($result90)){
$this->testaddressid = $row90[0];
$this->testaddressnote1 = $row90[2];
$query91 = "select * from address where addressid = '$testaddressid'";
$result91 = mysql_query($query91) or die(mysql_error());
if($row91 = mysql_fetch_array($result91)){
$this->teststreetid = $row91[1];
$this->teststreetnumber = $row91[2];
$testaddressnote2 = $row91[3];
$query92 = "select * from street where streetid = '$teststreetid'";
$result92 = mysql_query($query92) or die(mysql_error());
if($row92 = mysql_fetch_array($result92)){
$this->teststreetdirection = $row92[1];
$this->teststreetname = $row92[2];
$this->teststreettype = $row92[3];
$this->addresscomplete = 'Yes';
}
}
$address = new GetAddress();
$address->find(1);
print "<p>{$address->teststreetnumber} {$address->teststreetname} {$address->teststreettype}</p>";
Some error checking of those queries would be nice.
(#10850)