Link to home
Create AccountLog in
Avatar of breeze351
breeze351

asked on

Session vars

Stupid question.  I have allot on my plate today.
Can someone give me an answer on this code.  I know it's wrong but I don't have time to debug it.

$conn = mysql_connect('localhost', '$_SESSION[Local_Name]', '$_SESSION[Local_Password]');

I'm missing quotes somewhere.

Thanks
Glenn
Avatar of msifox
msifox
Flag of Germany image

The arguments with $_SESSION should not be in quotes, but the arguments in [brackets] probably should.
$conn = mysql_connect('localhost', $_SESSION['Local_Name'], $_SESSION['Local_Password']);
And you should switch to PDO because newer PHP will not support mysql_connect  any more.
Avatar of Dave Baldwin
I believe @msifox is right.  My normal connection line is:

mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
This will help you make the required conversion away from MySQL:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

This will help you avoid confusion about PHP's "squishy" syntax and lax data structures.

error_reporting(E_ALL);
Also it is important to understand the difference between single and double quotes in PHP

Single quotes - everything inside the quotes is treated as a literal - i.e. it comes out exactly as it looks.

Double quotes - php will interpret escaped characters and $variables and replace them with their appropriate counter parts so

$x = 'fred';

echo '$x'; // will output $x

Whereas

echo "$x"; // will output fred

The problem here is you used single quotes. Double quotes would have worked but as highlighted above they are unnecessary.

You also might want to move of mysql as this has been deprecated in future versions of MySQL support in PHP - consider using PDO or MySQLi
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks for the points, ~Ray