Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Full error: Parse error: syntax error, unexpected T_VARIABLE in /home/u572186424/public_html/safe.php on line 56

I have been staring at line 56 and cannot figure it out...

     exit();

The whole file follows:

<?php include_once("connect.php"); ?>
<?
$sql = "SELECT * FROM users WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$id = htmlspecialchars($row->id);
$userip = htmlspecialchars($row->userip);
$username = htmlspecialchars($row->username);
$password = htmlspecialchars($row->password);
$account_type = htmlspecialchars($row->account_type);
$money = htmlspecialchars($row->money);
$exp = htmlspecialchars($row->exp);
$req_exp = htmlspecialchars($row->req_exp);
$level = htmlspecialchars($row->level);
$health = htmlspecialchars($row->health);
$max_health = htmlspecialchars($row->max_health);
$lastactive = htmlspecialchars($row->lastactive);
$energy = htmlspecialchars($row->energy);
$max_energy = htmlspecialchars($row->max_energy);
$will = htmlspecialchars($row->will);
$max_will = htmlspecialchars($row->max_will);
$brave = htmlspecialchars($row->brave);
$max_brave = htmlspecialchars($row->max_brave);
$strength = htmlspecialchars($row->strength);
$agility = htmlspecialchars($row->agility);
$guard = htmlspecialchars($row->guard);
$labor = htmlspecialchars($row->labor);
$iq = htmlspecialchars($row->iq);
$rank = htmlspecialchars($row->rank);
?>
<?php
$sql = "SELECT * FROM sitestats WHERE id='1'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$admins = htmlspecialchars($row->admins);
$mods = htmlspecialchars($row->mods);
$hdo = htmlspecialchars($row->hdo);
$admins_ip = htmlspecialchars($row->admins_ip);
$mods_ip = htmlspecialchars($row->mods_ip);
$admin_array = explode("-", $admins);
$mod_array = explode("-", $mods);
$hdo_array = explode("-", $hdo);
$admin_ip_array = explode("-", $admins_ip);
$mod_ip_array = explode("-", $mods_ip);
?>
<html>
<body>
<?
if(isset($_SESSION['user_id'])) {
 $sql = "UPDATE users SET lastactive=NOW() WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
  mysql_query($sql);
}else{
   header("Location: logout.php");
     exit();  //ERROR HERE
}
$query = "SELECT account_type,rank FROM users WHERE username= "$username";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if($row['account_type'] == 1){
$row['rank'] = "Player";
$rank = "Player";
}elseif($row['account_type'] == 2){
$row['rank'] = "VIP";
$rank = "VIP";
}elseif($row['account_type'] == 3){
$row['rank'] = "HDO";
$rank = "HDO";
}elseif($row['account_type'] == 4){
$row['rank'] = "Moderator";
$rank = "Moderator";
}elseif($row['account_type'] == 5){
$row['rank'] = "Admin";
$rank = "Admin";
}elseif($row['account_type'] == 6){
$row['rank'] = "Owner";
$rank = "Owner";
}
?>
</body>
</html>

By the way, this code is in PHP if you haven't been able to tell. Please help out! Thanks!

share|improve this question
    
Error is here username= ".$username –  M Khalid Junaid Nov 14 '13 at 18:17
    
yo, indent that a little so we can actually read it? Also, reduce the code until the error goes away. When it does, what you just removed caused the error. Typically you do this first because you tend to find the problem before needing to post a question on it –  Mike 'Pomax' Kamermans Nov 14 '13 at 18:17
add comment

2 Answers

up vote 1 down vote accepted

The syntax highlighting shows you. Problem is the extra quote " here:

$query = "SELECT account_type,rank FROM users WHERE username= "$username";

Try:

$query = "SELECT account_type,rank FROM users WHERE username= '$username'";
share|improve this answer
    
Perfect! Thank you! –  Jacob Nov 14 '13 at 18:20
    
if your string is using double quotes, you can also write a variable without having to close the quotes, concating and opening again, like this: $query = "SELECT something FROM someTable WHERE condition = {$condition}"; –  Edson Horácio Junior Nov 14 '13 at 18:27
    
And notice the use of the single quotes ' as I assume username is text/varchar in the db. –  AbraCadaver Nov 14 '13 at 18:29
    
Yes, thanks for all the feedback. I can't believe I didn't see that :P –  Jacob Nov 14 '13 at 18:37
    
It was easy to spot the color change in the code that you posted, so try and get an editor with good syntax highlighting. –  AbraCadaver Nov 14 '13 at 18:41
add comment

There are no dots when you concatenate the query and $username:

$query = "SELECT account_type,rank FROM users WHERE username= "$username";
share|improve this answer
    
You don't need to concatenate a string with a variable containing a string in PHP. You can just do: $query = "SELECT account_type,rank FROM users WHERE username=$username"; –  Alex Kinnee Nov 14 '13 at 18:24
    
That's right, you either have to put your variable inside quotation marks, or concatenate it. Not the half way. –  regulus Nov 14 '13 at 19:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.