Syntax error, need help

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
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Syntax error, need help

Post by Getran »

I have a page that is supposed to show different things depending on the level the user's account has. But i keep getting this error:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

this is my script:

Code: Select all

<?php
 $checkcookie = $_COOKIE['terrastormlogin'];
    if (isset($checkcookie))
    {
 		$loggedin = '1';
		global $loggedin;
		global $user;
        require('db_info.php');
        mysql_connect("$host", "$username", "$password") or die(mysql_error());
 		mysql_select_db("$database") or die(mysql_error());
 		$checklevel = mysql_query("SELECT * from accounts WHERE username=$user") or die(mysql_error());
 		while ($fetch=mysql_fetch_array($checklevel))
        {
        	$level=$fetch['level'];
            $user=$fetch['username'];

            if ($level = 0)
            {
            	$maincontent = "Normal User";
            }
            else if ($level = 1)
            {
            	$maincontent = "Moderator";
            }
            else if ($level = 2)
            {
            	$maincontent = "Low Level Admin";
            }
            else if ($level = 3)
            {
            	$maincontent = "Admin";
            }
            else if ($level = 4)
            {
            	$maincontent = "Developer";
            }
            else if ($level = 5)
            {
            	$maincontent = "God!";
            }
        }

    }
    else if (isset($checkcookie))
    {
    	$loggedin = "0";
        global $loggedin;
        $maincontent = "";
        $maincontent .= "Cookie Not Set";
    }
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

put single quotes around $user, and make sure $user is actually set and working by echoing your sql query string prior to doing the actual query.
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post by Getran »

ok well i did what u said, and now it shows the page but it doesn't display ANY of the text ($maincontent). This is the script now :

Code: Select all

<?php
$maincontent = "";
 $checkcookie = $_COOKIE['terrastormlogin'];
    if (isset($checkcookie))
    {
 		$loggedin = '1';
		global $loggedin;
		global $user;
        require('db_info.php');
        mysql_connect("$host", "$username", "$password") or die(mysql_error());
 		mysql_select_db("$database") or die(mysql_error());
 		$checklevel = mysql_query("SELECT * from accounts WHERE username='$user'") or die(mysql_error());
 		while ($fetch=mysql_fetch_array($checklevel))
        {
        	$level=$fetch['level'];
            $dbuser=$fetch['username'];

            if ($level = 0)
            {
            	$maincontent .= 'Normal User';
            }
            else if ($level = 1)
            {
            	$maincontent .= 'Moderator';
            }
            else if ($level = 2)
            {
            	$maincontent .= 'Low Level Admin';
            }
            else if ($level = 3)
            {
            	$maincontent .= 'Admin';
            }
            else if ($level = 4)
            {
            	$maincontent .= 'Developer';
            }
            else if ($level = 5)
            {
            	$maincontent .= 'God!';
            }
        }

    }
    else if (!isset($checkcookie))
    {
    	$loggedin = "0";
        global $loggedin;
        $maincontent .= 'Cookie Not Set';
    }

?>
i just have: echo "$maincontent"; to display it. It works for all my other pages except this :?

*EDIT* and i put $maincontent = ""; at the top, because when it wasn't there i got this error:


Notice: Undefined variable: maincontent in *hidden*\portal.php on line 125
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

general debugging is in order then:

Code: Select all

//.....
echo $sql = "SELECT * from accounts WHERE username='$user'";       // added
$checklevel = mysql_query($sql) or die(mysql_error());                     // your line, modified
echo mysql_num_rows($checklevel);                                                // added
while ($fetch=mysql_fetch_array($checklevel))                                  // your line
{
  print_r($fetch);
//........
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post by Getran »

i rewrote the whole thing like this :

Code: Select all

<?php
checkcookie = $_COOKIE['terrastormlogin'];
    if (isset($checkcookie))
    {
    	$loggedin = "1";
			global $loggedin;
			global $user;
			require("db_info.php");
			mysql_connect($host,$username,$password) or die(mysql_error());
			mysql_select_db($database) or die(mysql_error());
			$sql = "SELECT * FROM `accounts` WHERE `username`='$user'";
			$query = mysql_query($sql) or die(mysql_error());
			while ($fetch=mysql_fetch_array($query))
			{
			 			$dbuser=$fetch['username'];
						$userlevel=$fetch['level'];
						
						$maincontent = "";
						$maincontent .= "$dbuser";
			}			
    }
    else if (!isset($checkcookie))
    {
    	$loggedin = "0";
			global $loggedin;
			$maincontent = "";
			$maincontent .= "Not logged in!";
    }
?>
But not all i'm getting is:

Notice: Undefined variable: maincontent in *hidden*\portal.php on line 97

*EDIT* - I changed the code again, and put in what you told me to, i still get the maincontent error. and i get: SELECT * FROM `accounts` WHERE `username`=''0 in the top corner, because it echo's it
Post Reply