Home > Archive > PHP Language > October 2006 > Returning one row from mySQL
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Returning one row from mySQL
|
|
| Simon Harris 2006-10-12, 6:58 pm |
| Hi All,
I am new to PHP, I'm have written a function to get a county name based on
an ID:
// Return a County name from a County ID
function GetCountyNameFromID($idCOunty) {
$sql = "SELECT county_name FROM VisitorWorld.counties where isCounty = "
..$idCounty. ;
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
return $row{'county_name'};
}
}
Obviously, this is looping needlessly - I know it will only return one
value.
Could someone please tell me how I can change this so I can remove the loop?
Thanks!
--
-
* Please reply to group for the benefit of all
* Found the answer to your own question? Post it!
* Get a useful reply to one of your posts?...post an answer to another one
* Search first, post later : http://www.google.co.uk/groups
* Want my email address? Ask me in a post...Cos2MuchSpamMakesUFat!
--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 10214 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!
| |
| Cheshmak 2006-10-12, 6:58 pm |
|
Simon Harris wrote:
> Hi All,
>
> I am new to PHP, I'm have written a function to get a county name based on
> an ID:
>
> // Return a County name from a County ID
> function GetCountyNameFromID($idCOunty) {
> $sql = "SELECT county_name FROM VisitorWorld.counties where
isCounty = "
> .$idCounty. ;
> $result = mysql_query($sql) or die(mysql_error());
> while($row = mysql_fetch_array($result)) {
> return $row{'county_name'};
> }
> }
>
> Obviously, this is looping needlessly - I know it will only return one
> value.
>
> Could someone please tell me how I can change this so I can remove the loop?
>
> Thanks!
Hi
Step 1:
remove "."(dot) at end of $idCounty. -----> replace $idCounty
with $idCounty. on line 3
Step 2:
--------
while($row = mysql_fetch_array($result)) {
return $row{'county_name'};
}
---------
only return first result. to return all ------------>
replace
$rslt[] = array();
while($row = mysql_fetch_array($result)) {
$rslt[] = $row['county_name'];
}
return $rslt;
with
> while($row = mysql_fetch_array($result)) {
> return $row{'county_name'};
> }
| |
| .:[ ikciu ]:. 2006-10-12, 6:58 pm |
| Hmm Cheshmak <cheshmak4net@gmail.com> wrote:[color=darkred]
if one row then dont use while - try to use is or just mysql_result
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl
2be || !2be $this => mysql_query();
| |
| Koncept 2006-10-12, 6:59 pm |
| In article <KSwWg.23861$w07.15778@newsfe6-win.ntli.net>, Simon Harris
<too-much-spam@makes-you-fat.com> wrote:
> Could someone please tell me how I can change this so I can remove the loop?
SELECT [...] LIMIT 1;
Since your id that you are passing is probably unique, you only need to
return one value. Next check that there is a value in the result. If
there is, process, or handle the error.
<?php
// Something like this ... consider this pseudo-code.
function gcnfid($id=null)
{
if(!isset($id)){ trigger_error("[...] no ID passed", E_USER_ERROR); }
$sql = "SELECT `country_name` FROM `VisitorWorld.countries` ";
$sql .= "WHERE `isCountry` = '{$idCountry}' LIMIT 1";
$result = @mysql_query($sql) || die(mysql_error());
$row = $mysql_fetch_array($result);
if($row) { return $row['country_name']; }
trigger_error("[...] no country found", E_USER_WARNING);
}
?>
--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
|
|
|
|
|