Jump to content

Strang MySQL Query Error


ionik

Recommended Posts

$sql = 'SELECT userid FROM '.USER_TABLE.' WHERE password = "'.$encrypted_pass.'" AND username = "'.$username.'"';

// SELECT userid FROM users WHERE password = "md5" AND username = "UserName"

// that the sql that is shown when the script is run 

if( $db->query($sql) )

 

for some reason i keep getting the 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

 

if i take out AND username = "'.$username.'"

 

it will work...but if i take out

 

password = "'.$encrypted_pass.'" AND

 

get that error again cant fiqure this one out....

Link to comment
Share on other sites

we can't really help you without knowing the values of the variables. after this line:

$sql = 'SELECT userid FROM '.USER_TABLE.' WHERE password = "'.$encrypted_pass.'" AND username = "'.$username.'"';

put:

print $sql;exit;

 

it will print the generated query. then, put that in a post

Link to comment
Share on other sites

fiqured out the problem.....seems to be a bug with MySQL....

if you try to get the value of a table where its name is the same as what your looking for will result in an error...?

hence

username = "username" results in an error

but

username = "name" works fine....strange

Link to comment
Share on other sites

I just created a MySQL table using:

CREATE TABLE `table` (
`userid` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 255 ) NOT NULL ,
`password` VARCHAR( 255 ) NOT NULL
);
INSERT INTO `table` (`username`,`password`) VALUES ('UserName','1a3c9b664bf1869546f449522894e5b90e70b99d');

 

then ran the script:

<?php
  include('db_connect.php');
  define(USER_TABLE,'users');
  $encrypted_pass = '1a3c9b664bf1869546f449522894e5b90e70b99d';
  $username = 'UserName';
  $sql = 'SELECT userid FROM '.USER_TABLE.' WHERE password = "'.$encrypted_pass.'" AND username = "'.$username.'"';
  $r = mysql_query($sql) or die ("Query failed: ".mysql_error());
  print_r(mysql_fetch_assoc($r));
?>

 

It produced no error and the expected results.

 

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.