Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    efficient mySQL selects using PHP

    what's the most efficient way to grab a single data value from a table field using PHP?

    the best I have is using a while loop -- but something tells me there is a cleaner/more efficient way:

    Code:
    $sql="select countryType from c_countries where userid={$_SESSION[userid]}";
    $result=mysql_query($sql);
    while($row=mysql_fetch_array($result)){ $userCountryType = $row[countryType]; }

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Have a look at mysql_result()

    PHP Code:
    $sql="select countryType from c_countries where userid={$_SESSION[userid]}";
    $result=mysql_query($sql);
    $userCountryType mysql_result($result,0,0); //the last parameter depends on how the DB table is set up.  This will return the first field of the first row. 
    Last edited by StupidRalph; 07-12-2007 at 06:00 PM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    StupidRalph is spot on... just keep in mind you don't need a loop unless the query results are going to produce more than one row, so using mysql_fetch_array() without the while loop would also be fine.

  • #4
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Quote Originally Posted by Fumigator View Post
    StupidRalph is spot on... just keep in mind you don't need a loop unless the query results are going to produce more than one row, so using mysql_fetch_array() without the while loop would also be fine.
    I've recently reached my 500 post. I know what level you and the rest of the "Senior Coders" post. I don't want to hurt the ranking system's credibility too much.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •