phantombmx
2010-12-05 23:35:08
I am using MySQLi for some database handling and I don't understand why my code isn't working. This is really my first time using prepared statements, but it would be beneficial for me in this particular situation.
[PHP]// new connection
$mysqli = new mysqli(
$config['database']['connect']['host'],
$config['database']['connect']['username'],
$config['database']['connect']['password'],
$config['database']['connect']['name']
);
// verify connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// create prepared statement
if ($stmt = $mysqli->prepare('SELECT password FROM login_users WHERE user_name = ? LIMIT 1')) {
$stmt->bind_param('s', $username);
$username = 'gooduser';
$password = md5('goodpass');
$stmt->execute();
$stmt->store_result();
$_SESSION['messages'][] = 'Num rows: ' . $stmt->num_rows;
$stmt->bind_result($pass);
$_SESSION['messages'][] = 'Line 67';
while ($stmt->fetch()) {
$_SESSION['messages'][] = 'Line 69';
if ($password == $pass) {
$_SESSION['messages'][] = 'Success!';
}
else {
$_SESSION['messages'][] = 'Bad pass';
}
}
$_SESSION['messages'][] = 'Line 77';
header('Location: ' . $_SESSION['redirect']);
$stmt->close();
}
$mysqli->close();
[/PHP]With a username of "gooduser" and "goodpass", this should return the correct row and it should work from there. Note that $_SESSION['messages'] is my message tracker. When outputting the messages, I get this output:
[PHP]// create prepared statement
if ($stmt = $mysqli->prepare('SELECT password FROM login_users WHERE user_name = "gooduser" LIMIT 1')) {
//$stmt->bind_param('s', $username);
//$username = 'gooduser';
$password = md5('goodpass');
[/PHP]This returns, as intended:
Here is the table structure:
[PHP]// new connection
$mysqli = new mysqli(
$config['database']['connect']['host'],
$config['database']['connect']['username'],
$config['database']['connect']['password'],
$config['database']['connect']['name']
);
// verify connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// create prepared statement
if ($stmt = $mysqli->prepare('SELECT password FROM login_users WHERE user_name = ? LIMIT 1')) {
$stmt->bind_param('s', $username);
$username = 'gooduser';
$password = md5('goodpass');
$stmt->execute();
$stmt->store_result();
$_SESSION['messages'][] = 'Num rows: ' . $stmt->num_rows;
$stmt->bind_result($pass);
$_SESSION['messages'][] = 'Line 67';
while ($stmt->fetch()) {
$_SESSION['messages'][] = 'Line 69';
if ($password == $pass) {
$_SESSION['messages'][] = 'Success!';
}
else {
$_SESSION['messages'][] = 'Bad pass';
}
}
$_SESSION['messages'][] = 'Line 77';
header('Location: ' . $_SESSION['redirect']);
$stmt->close();
}
$mysqli->close();
[/PHP]With a username of "gooduser" and "goodpass", this should return the correct row and it should work from there. Note that $_SESSION['messages'] is my message tracker. When outputting the messages, I get this output:
Num rows: 0
Line 67
Line 77
It is skipping the while-loop because Num rows is 0. When changing the snippet by the query to not use bound params, it works:[PHP]// create prepared statement
if ($stmt = $mysqli->prepare('SELECT password FROM login_users WHERE user_name = "gooduser" LIMIT 1')) {
//$stmt->bind_param('s', $username);
//$username = 'gooduser';
$password = md5('goodpass');
[/PHP]This returns, as intended:
Num rows: 1
Line 67
Line 69
Success!
Line 77
Anyone care to explain what I am doing wrong here? I am attempting to make a simple login script and I am (clearly) failing at it.Here is the table structure:
CREATE TABLE `login_users` (
`user_id` int(11) NOT NULL auto_increment,
`user_name` varchar(30) NOT NULL default '',
`password` varchar(70) NOT NULL default '',
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;