PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Dec 22, 2014 3:27 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Sep 21, 2011 3:19 pm 
Offline
Forum Newbie

Joined: Sun Sep 18, 2011 6:51 am
Posts: 5
Hello.

I am having trouble using session-variables when they are needed to receive data from a table.
If I put the variable $FirstName, between single or double quotes in attempt to get some data from a table players, I get an error saying:
Parse error: syntax error, unexpected T_VARIABLE... on the code-line I've commented.
If I skip the quotes around the $FirstName, I get en error on the next code-line, stating:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.

Syntax: [ Download ] [ Hide ]
<?PHP
session_start();

  //putting variables from the session-array into local variables
$FirstName = $_SESSION['GameName'][0];
$LastName = $_SESSION['GameName'][1];
$GameName = $FirstName . " " . $LastName;
echo "$GameName";      //this is echoing normally, indicating that the code is working so far.

  //connecting to database
  mysql_connect(****, *****, *****) or die(mysql_error());
  mysql_select_db(****) or die(mysql_error());

  //extracting data from the result-array
  $result1 = mysql_query('SELECT Funds, Salory FROM Players WHERE FirstName="$FirstName" '); //here is some kind of trouble..
  $row1 = mysql_fetch_array($result1);

echo $row1['Funds'];
echo $row1['Salory'];

?>
 


Any ideas?


Top
 Profile  
 
PostPosted: Wed Sep 21, 2011 4:42 pm 
Offline
Forum Regular
User avatar

Joined: Tue Sep 28, 2010 11:41 am
Posts: 968
Location: Columbus, Ohio
You can only place variables inside of double quotes, not single quotes. (Single quotes also will not evaluate escaped characters like \n )

While you do have it in double quotes at first glance, keep in mind those quotes are part of the SQL statement, not the PHP code. You would need to do one of these two:
Syntax: [ Download ] [ Hide ]
$result1 = mysql_query('SELECT Funds, Salory FROM Players WHERE FirstName="'.$FirstName.'" ');

Syntax: [ Download ] [ Hide ]
$result1 = mysql_query("SELECT Funds, Salory FROM Players WHERE FirstName=\"$FirstName\" ");


Also, if you go the double quote route (second example above), you can directly use the $_SESSION if you want by wrapping it with { } 's
Syntax: [ Download ] [ Hide ]
$result1 = mysql_query("SELECT Funds, Salory FROM Players WHERE FirstName=\"{$_SESSION['GameName'][0]}\" ");


-Greg


Top
 Profile  
 
PostPosted: Thu Sep 22, 2011 12:27 pm 
Offline
Forum Newbie

Joined: Sun Sep 18, 2011 6:51 am
Posts: 5
Great! Thanks, it works perfect.


Top
 Profile  
 
PostPosted: Thu Sep 22, 2011 2:56 pm 
Offline
Forum Newbie

Joined: Sun Sep 18, 2011 6:51 am
Posts: 5
A follow-up question:
Why isn't this working?
Syntax: [ Download ] [ Hide ]
....
//connected to database

$FirstName = $_SESSION['GameName'][0];
$tablename = $_SESSION['table'][0];

$result = mysql_query('SELECT Funds, Salory FROM "'.$tablename.'" WHERE FirstName="'.$FirstName.'" '); //something wrong where it says $tablename, because if I simply insert the name of the table, everything works.
$row = mysql_fetch_array($result);

//retrieving data from $row
....
 


I've tried so many different quote-combinations in the query-line. Can't get it to work.
Any ideas?


Top
 Profile  
 
PostPosted: Thu Sep 22, 2011 4:54 pm 
Offline
Forum Regular
User avatar

Joined: Tue Sep 28, 2010 11:41 am
Posts: 968
Location: Columbus, Ohio
Database/Table/Field names are not wrapped with double or single quotes, they are wrapped with backticks (`, on a PC to the left of the 1 key):

Syntax: [ Download ] [ Hide ]
SELECT `tblSample`.`SampleField` FROM `tblSample` LEFT JOIN `tblExample` ON `tblExample`.`SampleID` = `tblSample`.`ID` WHERE `tblExample`.`Field3`='Whatever'


That is for example purpose, much better to actually write:

Syntax: [ Download ] [ Hide ]
SELECT s.`SampleField` FROM `tblSample` AS s LEFT JOIN `tblExample` AS e ON e.`SampleID` = s.`ID` WHERE e.`Field3`='Whatever'


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group