MySQL/Variable Trouble -SOLVED!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Zeyber
Forum Newbie
Posts: 16
Joined: Wed Sep 26, 2007 9:23 pm

MySQL/Variable Trouble -SOLVED!

Post by Zeyber »

I always get this error when I do this?

Code: Select all

//Look for Username in CE
$CEuser = mysql_query("SELECT * FROM CE 
WHERE Name='$_SESSION['username']'");
The error is:


Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/www/warriorwars.freehostia.com/game/game.php on line 35

I don't know why it is happening or how to fix it.
Last edited by Zeyber on Sun Sep 30, 2007 10:27 pm, edited 1 time in total.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Does this work?

Code: Select all

//Look for Username in CE
$CEuser = mysql_query("SELECT * FROM CE WHERE Name='{$_SESSION['username']}'");
Also you should think about passing $_session['username'] by mysql_escape_string(), even if you've validated it.
Zeyber
Forum Newbie
Posts: 16
Joined: Wed Sep 26, 2007 9:23 pm

Post by Zeyber »

What ashame it didn't work :( Something fishy is going on I think. I put a space on the line where the error was from line 35 to 36, and I get the exact same error on line 35.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Parse errors don't necessarily occur ON the line that the parser tells you, and oftentimes is the result of the line before it. Show us a few lines before and after this one. Also, make sure you use

Code: Select all

tags, because the highlighting can help you notice the error yourself.
Zeyber
Forum Newbie
Posts: 16
Joined: Wed Sep 26, 2007 9:23 pm

Post by Zeyber »

Ok here is everything:

Code: Select all

<?php
session_start();
?>

<html>
<body bgcolor="#000000">
<center>
<img src="logo.gif">
<font color="#FFFFFF">
<p>
</html>

<?php
//Get Username or Send Home
if(isset($_SESSION['username']))
 {
 echo $_SESSION['username'];
 }
else
 {
 echo '<script> location.href = "http://freehostia.com"; </script>';
 }
$username=$_SESSION['username'];

//Connect to MySQL
$con = mysql_connect("dbhost","dbuser","dbpass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("warwar63_ww", $con);

//Look for Username in CE
$ceuser = mysql_query("SELECT * FROM CE WHERE Name='{$_SESSION['username']}'");

//Check if New User
If ($ceuse=null)
{
mysql_query("INSERT INTO CE (Name, Weapon, Shield, Body, Helmet, Legs, Necklace, Gloves, Boots, Ring, Earring, Cape, Symbol, Bangle, Mount, Wings) 
VALUES ('$username', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')");
}

mysql_close($con);
?>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

I've copy-pasted your code but there were no syntax errors.
The only error I see is that you use "=" instead of "==" in your last IF statement.

EDIT: And having "$ceuser==null" doesn't mean that there are no records ... it means that you have SQL syntax error.
Last edited by VladSun on Sun Sep 30, 2007 4:33 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I got no parse errors. On a side note, your last if statement uses "$ceuse" instead of "$ceuser" and also uses "=" instead of "==."
Zeyber
Forum Newbie
Posts: 16
Joined: Wed Sep 26, 2007 9:23 pm

Post by Zeyber »

Ok I think something has gone wrong with my host because whatever I do I get exactly the same error:

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/www/warriorwars.freehostia.com/game/game.php on line 35

Anyone got a solution?

EDIT: Lol I figured it out! I was editing the wrong file. There was game.php and game/game.php and I was doing stuff to the wrong one. All better now. Thanks for the help!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Are you sure that you're showing us the code from the correct file?

/home/www/warriorwars.freehostia.com/game/game.php ?


I used to help out someone who had a freehostia account and they allow you to have multiple sites (I think) and he ended up with different copies of the same stuff around, which ended up being slightly confusing.
Zeyber
Forum Newbie
Posts: 16
Joined: Wed Sep 26, 2007 9:23 pm

Post by Zeyber »

Well I don't get errors now but I just don't know how to do this properly:

Code: Select all

//Look for Username in CE
$CEuser = mysql_query("SELECT * FROM CE WHERE Name='{$_SESSION['username']}'");

//Check if New User
If ($CEuser==NULL)
{
mysql_query("INSERT INTO CE (Name, Weapon, Shield, Body, Helmet, Legs, Necklace, Gloves, Boots, Ring, Earring, Cape, Symbol, Bangle, Mount, Wings) 
VALUES ('{$_SESSION['username']}', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')");
}
I want it to look for the username in a table and if it doesn't exist, to add it to the table.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, the first thing that you have to realize is that $CEuser is nothing more than a MySQL resource. In this case, mysql_num_rows() should help you out.
Zeyber
Forum Newbie
Posts: 16
Joined: Wed Sep 26, 2007 9:23 pm

Post by Zeyber »

Ah I see what you mean. Thanks I got it all working. I used the fetch array function.

Code: Select all

//Look for Username in CE
$CEuser = mysql_query("SELECT * FROM CE WHERE Name='{$_SESSION['username']}'");
$CEname = mysql_fetch_array($CEuser);

//Check if New User
If ($CEname==NULL)
{
mysql_query("INSERT INTO CE (Name, Weapon, Shield, Body, Helmet, Legs, Necklace, Gloves, Boots, Ring, Earring, Cape, Symbol, Bangle, Mount, Wings) 
VALUES ('{$_SESSION['username']}', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')");
}
Post Reply