Jump to content

PDO Exception and object problem


Eggzorcist

Recommended Posts

Hey, I'm wondering what the issue is, I'm new to using PDO and objects so I'm sure this is a common error but setting me on the right track would be greatly appreciated.

 

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'AND password = d41d8cd98f00b204e9800998ecf8427e' at line 1' in /Users/JPFoster/Sites/Jaipai.Blog/engine.php:47 Stack trace: #0 /Users/JPFoster/Sites/Jaipai.Blog/engine.php(47): PDO->query('SELECT * FROM u...') #1 /Users/JPFoster/Sites/Jaipai.Blog/index.php(7): SiteEngine->login('', '') #2 {main} thrown in /Users/JPFoster/Sites/Jaipai.Blog/engine.php on line 47

public function login($user, $pw){
	$user = addslashes($this->user);
	$pw = md5($pw);

	$query = "SELECT * FROM users WHERE username = ".$user." AND password = ".$pw;

	//set up query
	$results = $this->pdo->query($query);
	$num_rows = $results->num_rows;

	if ($num_rows == 1){
	$row = $results->fetch_assoc();
	echo "Worked!";
		//$this->set_session($row['id'], $row['username'], $row['email']);
	} else {

	echo "Your Username and Password did not match"; 	

	}

	//if user + passs match { redirect (member page) and set up sessions

	// Else ... Retry...
}	


}

 

I don't see an issue with my query, and the object is instantiated by having the submit button isset() and i use login($_post[user], $_post[pw])

 

 

Link to comment
Share on other sites

The exception is telling you that you have a syntax error in your query. This could be due to an escape character being passed into the user variable. Try doing:

$query = "SELECT * FROM `users` WHERE `username` = '".$user."' AND `password` = '".$pw."'";

Also try and echo out

$query

to see what the query looks like

Link to comment
Share on other sites

That has helped a lot! Thanks, but now however, I have trouble with my PDO num_rows, is there a reason why its a fairly simplistic code so I'm not sure why it wouldn't work.

 

 

Notice: Undefined property: PDOStatement::$num_rows in /Users/JPFoster/Sites/Jaipai.Blog/engine.php on line 48

 

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.