Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

php - Call to a member function bindParam() on a non-object

I'm working a log-in script that I found on the web. (http://alias.io/2010/01/store-passwords-safely-with-php-and-mysql/)

As I was trying to make it work, I stumbled upon this error:

bool(false) Fatal error: Call to a member function bindParam() on a non-object in /srv/disk3/1446018/www/askmephilosophy.co.nf/session.php on line 25

My code:

<?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';
  }

?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

your connection object is mysqli, you should use PDO to connect mysql like

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...