gpt4 book ai didi

php - 调用非对象的成员函数 bindParam()

转载 作者:行者123 更新时间:2023-11-29 00:27:09 25 4
gpt4 key购买 nike

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

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

bool 值(假) fatal error :在第 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();
}

关于php - 调用非对象的成员函数 bindParam(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18401682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com