Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

carlosbutler

macrumors 6502a
Original poster
Feb 24, 2008
691
2
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
 
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.

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"];
 
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"];
 
So is there, for example, a difference between $hotelSQLID=$_SESSION['hotel'];
and
$hotelSQLID=$_SESSION["hotel"];

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
 
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"];

No quotes is better -- no interpolation required.

PHP:
  $something=$hotelBookingRow[$hotelBookingRoom];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.