MySQLi SELECT 绑定参数不返回行
我正在使用 MySQLi 进行一些数据库处理,但我不明白为什么我的代码不起作用。
// 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();
使用用户名“gooduser”和“goodpass”,这应该返回正确的行,并且应该从那里开始工作。请注意,$_SESSION['messages'] 是我的消息跟踪器。输出消息时,我得到以下输出:
Num rows: 0
Line 67
Line 77
它正在跳过 while 循环,因为 Num rows 为 0。 当通过查询更改片段以不使用绑定参数时,它会起作用:
// 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');
这返回:
Num rows: 1
Line 67
Line 69
Success!
Line 77
有人愿意解释我在这里做错了什么吗?我正在尝试制作一个简单的登录脚本。
编辑:这里是表结构:
-- Table structure for table `login_users`
--
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 ;
更新:通过各种来源,看起来 MySQLi 的完整功能集在 v4.1.3 中可用,尽管文档说明了 v4.1。有些功能可以工作,但其他功能(例如bind_param)则不能。我已经与我的主机交谈过,我希望升级到最新版本。
I am using MySQLi for some database handling and I don't understand why my code isn't working.
// 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();
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:
// 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');
This returns:
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.
EDIT: Here is the table structure:
-- Table structure for table `login_users`
--
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 ;
UPDATE: Through various sources, it looks like the full feature set of MySQLi is available in v4.1.3, despite the documentation stating v4.1. Some features work, but others (such as bind_param) do not. I've already talked to my host and I am looking to upgrade to the latest version.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在调用
$stmt->bind_param('s', $username); 之前设置
,经过测试并且可以工作。$username = 'gooduser';
编辑
对于我来说,以下工作,请将其复制粘贴到您的文件上,然后查看您得到的输出
返回的内容:
You need to set
$username = 'gooduser';
before you call$stmt->bind_param('s', $username);
, tested and works .EDIT
for me the following works , please copy paste it over you're file and see what output you get
returns :