MySQLi SELECT 绑定参数不返回行

发布于 2024-10-06 00:16:44 字数 2247 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2024-10-13 00:16:44

您需要在调用 $stmt->bind_param('s', $username); 之前设置 $username = 'gooduser';
,经过测试并且可以工作。

编辑
对于我来说,以下工作,请将其复制粘贴到您的文件上,然后查看您得到的输出

    <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    // 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')) {
        $username = 'gooduser';
 $password = md5('goodpass');
     $stmt->bind_param('s', $username);


        $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();
    var_dump($_SESSION);

返回的内容:

    array(1) {
      ["messages"]=>
      array(5) {
        [0]=>
        string(11) "Num rows: 1"
        [1]=>
        string(7) "Line 67"
        [2]=>
        string(7) "Line 69"
        [3]=>
        string(8) "Success!"
        [4]=>
        string(7) "Line 77"


 }
}

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

    <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    // 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')) {
        $username = 'gooduser';
 $password = md5('goodpass');
     $stmt->bind_param('s', $username);


        $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();
    var_dump($_SESSION);

returns :

    array(1) {
      ["messages"]=>
      array(5) {
        [0]=>
        string(11) "Num rows: 1"
        [1]=>
        string(7) "Line 67"
        [2]=>
        string(7) "Line 69"
        [3]=>
        string(8) "Success!"
        [4]=>
        string(7) "Line 77"


 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文