微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 在非对象上调用成员函数bindParam()

我正在使用我在网上找到的登录脚本. (http://alias.io/2010/01/store-passwords-safely-with-php-and-mysql/)

当我试图让它工作时,我偶然发现了这个错误

布尔(假)
致命错误:在第25行的/srv/disk3/1446018/www/askmephilosophy.co.nf/session.PHP调用非对象上的成员函数bindparam()

我的代码

<?PHP

  include('config.PHP');

  //  Establishing the connection:
  $MyConnection = MysqLi_connect('hostname', 'user', 'pass', 'database')
    or die('An error has occured when you were trying to login. You can return to the main page
      by clicking: <a href="index.PHP">here</a>. <br />
      If this error is consistent, please <a href="contact.PHP">contact us</a>.');

  //  information provide by the user:
  $username = $_POST['username'];
  $password = $_POST['password']; // Text version.

  $StatementHandle = $MyConnection->prepare('
    SELECT
      hash
    FROM Users
    WHERE
      username = :username
    LIMIT 1
    ');

  var_dump($StatementHandle);
  $StatementHandle->bindParam(':username', $username);

  $StatementHandle->execute();

  $user = $StatementHandle->fetch(PDO::FETCH_OBJ);

  // Hashing the password with its hash as the salt returns the same hash:
  if(crypt($password, $user->hash) == $user->hash) {
    echo 'You are logged in. Well, not actually. We only just confirmed that
      our system works. This service\'ll cost you $1';
  }

?>

解决方法:

你的连接对象是MysqLi,你应该使用PDO连接MysqL之类的

$dsn = 'MysqL:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection Failed: ' . $e->getMessage();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐