2013-08-23 33 views
1

我正在使用我在網上找到的登錄腳本。 (http://alias.io/2010/01/store-passwords-safely-with-php-and-mysql/在非對象上調用成員函數bindParam()

當我試圖使它的工作,我偶然發現了這個錯誤:

布爾(假) 致命錯誤:調用一個成員函數bindParam()在/ SRV一個非對象/disk3/1446018/www/askmephilosophy.co.nf/session.php在線25

我的代碼:

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

?> 
+2

你的mysqli連接不是一個對象,你已經使用了程序風格 –

+0

那麼我怎樣才能使它成爲一個對象呢? 只需使用PDO方法連接到數據庫? –

回答

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

他不是被迫的。 Mysqli也有約束參數。但是,它不是連接到mysqli對象的正確方法。此外,他混合PDO/mysqli –

+1

是的,但他後來使用pdo樣式綁定了幾行。如果他打算使用mysqli,他應該在mysqli_stmt對象上使用bind_param方法 – engvrdr

相關問題