Jump to content

[SOLVED] SQL Syntax error


berry05

Recommended Posts

I find that this works better for me:

$otherusername = "SELECT item FROM users_items WHERE username='".$Username."'";

Make sure that you check your spelling (field name, table name ... etc) and the variable $Username exists (it isn't actually $username) and it isn't empty or NULL.

Link to comment
Share on other sites

I find that this works better for me:

$otherusername = "SELECT item FROM users_items WHERE username='".$Username."'";

Make sure that you check your spelling (field name, table name ... etc) and the variable $Username exists (it isn't actually $username) and it isn't empty or NULL.

 

And how exactly does that work better? There was nothing wrong with the way it was before.  Variables are parsed when they are put inside double quotes. 

Link to comment
Share on other sites

ok i did what you guys told me to do and  now i dont get a error which is good but no items show up in inventory...

 

<?php session_start();

if(isset($_SESSION['otherusername'])){

$db=mysql_connect('localhost', 'root', '');

$res=mysql_select_db('textgame',$db) or die(mysql_error());

    
$otherusername = "SELECT item FROM users_items WHERE username='".$Username."'";
    
    $res=mysql_query($otherusername)or die(mysql_error());
    
  
    while($row = mysql_fetch_assoc($res)){


     echo $otherusername;
  
  }
}else{
   
   echo "Sorry your not a member please join us!";
}
}
?>

Link to comment
Share on other sites

I find that this works better for me:

$otherusername = "SELECT item FROM users_items WHERE username='".$Username."'";

Make sure that you check your spelling (field name, table name ... etc) and the variable $Username exists (it isn't actually $username) and it isn't empty or NULL.

 

And how exactly does that work better? There was nothing wrong with the way it was before.  Variables are parsed when they are put inside double quotes.

 

they are also in single quotes inside the doubble quotes...don't know, old habbits are hard to get rid of :)

Link to comment
Share on other sites

well you were supposed to echo it and post what it printed, for debugging purposes.  Putting it inside your while loop is not the most ideal place to do this, though it would serve the purpose.

 

nothing came out when i debugged it..i get no errors but the inventory doesnt show the item in my users_items table

Link to comment
Share on other sites

<OT>

$x = "one";
$string = "the number $x"; // output: the number one
$string = "the number '$x'"; // output: the number 'one'
$string = 'the number $x'; // output: the number $x
$string = 'the number "$x"'; // output: the number "$x"

</OT>

 

well you were supposed to echo it and post what it printed, for debugging purposes.  Putting it inside your while loop is not the most ideal place to do this, though it would serve the purpose.

 

nothing came out when i debugged it..i get no errors but the inventory doesnt show the item in my users_items table

 

Okay so you are telling me that when you do this:

 

$otherusername = "SELECT item FROM users_items WHERE username='".$Username."'";
echo $otherusername;

 

Nothing echoes out?

Link to comment
Share on other sites

ok ...i'll read the book...sorry man...

 

btw when i switch $otherusername with this .$_SESSION['otherusername'].

 

i get this echoed...

 

SELECT item FROM users_items WHERE username='SELECT item FROM users_items WHERE username='1111''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 '1111''' at line 1

Link to comment
Share on other sites

ok ...i'll read the book...sorry man...

 

btw when i switch $otherusername with this .$_SESSION['otherusername'].

 

i get this echoed...

 

SELECT item FROM users_items WHERE username='SELECT item FROM users_items WHERE username='1111''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 '1111''' at line 1

 

Show your code that makes that message appear.

Link to comment
Share on other sites

try and see if anything different happens.

<?php session_start();

if(isset($_SESSION['otherusername'])){

$db=mysql_connect('localhost', 'root', '');

$res=mysql_select_db('textgame',$db) or die(mysql_error());

    
$otherusername = $_SESSION['otherusername']; //"SELECT item FROM users_items WHERE username='".$Username."'";
    
    $res=mysql_query($otherusername)or die(mysql_error());
    
  
    while($row = mysql_fetch_assoc($res)){


     echo $otherusername;
     echo $row['item'] . "<BR />";
  
  }
}else{
   
   echo "Sorry your not a member please join us!";
}
}
?>

Link to comment
Share on other sites

part of my index2.php code...just the inventory part..

 

<?php session_start();

if(isset($_SESSION['otherusername'])){

$db=mysql_connect('localhost', 'root', '');

$res=mysql_select_db('textgame',$db) or die(mysql_error());

    
$otherusername = "SELECT item FROM users_items WHERE username='".$_SESSION['otherusername']."'";
echo $otherusername;
    
    $res=mysql_query($otherusername)or die(mysql_error());
    
  
    while($row = mysql_fetch_assoc($res)){


     echo $row;
  
  }
}else{
   
   echo "Sorry your not a member please join us!";
}
}
?>

 

insert2.php code

 

<?php
//ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="textgame"; // Database name
$tbl_name="users"; // Table name 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$Username = (isset($_POST['username'])) ? mysql_real_escape_string($_POST['username']) : FALSE;
$password1 = (isset($_POST['password1'])) ? $_POST['password1'] : FALSE;
$password2 = (isset($_POST['password2'])) ? $_POST['password2'] : FALSE;
if(!$Username) die('username not set');
if(!$password1) die('Password1 not set');
else $password1 = md5($password1);
if(!$password2) die('Password2 not set');
else $password2 = md5($password2);
$query = "SELECT * FROM users WHERE username='$Username' AND password1='$password1' AND password2='$password2'";
$result = mysql_query($query) or die( mysql_error());
$count=mysql_num_rows($result);
if($count==1)
{
$otherusername = "SELECT item FROM users_items WHERE username='".$Username."'";
session_start();
//session_register("username");
$_SESSION['Username'] = $Username;
$_SESSION['otherusername'] = $otherusername;

$result1 = mysql_query($queryy);
$row = mysql_fetch_assoc($result1);
$_SESSION['gold'] = $row['gold'];
//session_register("inventory");
$Query = "SELECT item FROM users_items WHERE username='$Username'";
$Result = mysql_query($Query);
$Row = mysql_fetch_array($Result);
$_SESSION['inventory'] = $Row['item'];
  

header("location:index2.php"); 
}
else {
echo "Wrong Username or Password";
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.