MySQL value into PHP variable

Discussion in 'Web Design and Development' started by carlosbutler, Apr 9, 2009.

  1. carlosbutler macrumors 6502a

    carlosbutler

    Joined:
    Feb 24, 2008
    Location:
    London City
    #1
    I am trying to transfer a number that I get from my SQL statement. I know that the statement will only come up with one row of information:

    PHP:
    <?    
    if(
    $_SESSION["hotelRoom"]=="single"){ $hotelBookingRoom='singleroom';}
    if(
    $_SESSION["hotelRoom"]=="double"){ $hotelBookingRoom='doubleroom';}
    $hotelSQLID=$_SESSION['hotel'];

    // SQL connection
    $myConn=mysql_connect("databaselocation","user","password");
    mysql_select_db("database");
    $hotelBookingStrSQL="select $hotelBookingRoom from hotel where Id='$hotelSQLID'";
    $hotelBooking=mysql_query($hotelBookingStrSQL,$myConn);
                            
    where($hotelBookingRow=mysql_fetch_array($hotelBooking)){
    $something=$hotelBookingRow['$hotelBookingRoom'];
    }
    echo 
    $something;
    ?>
    As seen, a variable in the session already has the room type, and got the SQL statement to work of that. the same applies for the $hotelSQLID. But I cant seem to get the $something to get the value. The $hotelBookingRow['$hotelBookingRoom'] either means $hotelBookingRow['singleroom'] or ...['doubleroom'] as these are the field names, with each field that has a number only which is what i am trying to get out.

    thanks,
    carlos
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    You need to use double quotes around $hotelBookingRoom in order for PHP to use the value of it rather than treating it as a string literal.
    PHP:
      $something=$hotelBookingRow["$hotelBookingRoom"];
     
  3. carlosbutler thread starter macrumors 6502a

    carlosbutler

    Joined:
    Feb 24, 2008
    Location:
    London City
    #3
    Thanks soooo much :D:D:D:D
    I think you answered another one of my previous questions, as you can tell im a PHP 'noob'.
    So is there, for example, a difference between $hotelSQLID=$_SESSION['hotel'];
    and
    $hotelSQLID=$_SESSION["hotel"];
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    When PHP encounters a pair of double quotes it will parse the string looking for anything it needs to interpret. With single quotes it just sees it as a string literal and won't process anything. So, PHP can process text inside single quotes a tad faster than double quotes, which means it's best to use single quotes when you don't need PHP to interpret any variables. Though, the speed difference is pretty minimal so it doesn't hurt anything to use double quotes on pure text.

    Additional reading:
    http://www.jonlee.ca/php-tidbit-single-quotes-vs-double-quotes/
    http://www.linuxformat.co.uk/wiki/index.php/PHP_-_Performance_tuning#Single_quotes_are_faster.21
     
  5. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #5
    No quotes is better -- no interpolation required.

    PHP:
      $something=$hotelBookingRow[$hotelBookingRoom];
     
  6. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #6
    Beat me to it. That's exactly right, savar, and thank you for posting that.
     
  7. carlosbutler thread starter macrumors 6502a

    carlosbutler

    Joined:
    Feb 24, 2008
    Location:
    London City

Share This Page