PHP mysqli_query()到PDO

时间:2016-09-05 08:00:19

标签: php mysql

我正在尝试创建一个页面来更改数据库中的密码。 我创建了表单,这是PHP代码:

if(isset($_POST['btn-newpass']))
{
$username = strip_tags($_POST['username']);
$password = md5(strip_tags($_POST['password']));
$password_new = md5(strip_tags($_POST['password_new']));
$password_new_conf = md5(strip_tags($_POST['password_new_conf']));
$password_in_db= mysqli_query("SELECT password FROM utilizatori WHERE username='$username'");

if(!$password_in_db)
{ echo "The entered username doesn't exist";}
elseif($password!=$password_in_db)
{ echo "The current password is wrong";}

if($password_new == $password_new_conf)
{$sql = mysqli_query("UPDATE utilizatori SET password='$password_new' WHERE username='$username'");}

if($sql)
{ echo "Changed successfully!";}
 else
{ echo "The passwords do not match";}
}

当我尝试更改密码时,出现以下错误:

Warning: mysqli_query() expects at least 2 parameters, 1 given in      A:\XAMPP\htdocs\testing\change_password.php on line 10
The entered username doesn't exist
Warning: mysqli_query() expects at least 2 parameters, 1 given in A:\XAMPP\htdocs\testing\change_password.php on line 18
Passwords do not match

在connection.php中,我有以下代码:

class Database
{   
private $host = "localhost";
private $db_name = "atlx";
private $username = "root";
private $password = "";
public $conn;

public function dbConnection()
{

    $this->conn = null;    
    try
    {
        $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
        $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
    }
    catch(PDOException $exception)
    {
        echo "Connection error: " . $exception->getMessage();
    }

    return $this->conn;
}
}

有人能指出我这里有什么问题吗?

编辑:

我意识到使用PDO完成了与数据库的连接。如何将PHP代码转换为与PDO一起使用?

5 个答案:

答案 0 :(得分:0)

在mysqli_query中,您必须传递连接变量以执行

替换

$password_in_db= mysqli_query("SELECT password FROM utilizatori WHERE username='$username'")

使用

$password_in_db= mysqli_query($con,"SELECT password FROM utilizatori WHERE username='".$username."'")

答案 1 :(得分:0)

mysqli_query($Database->dbConnection(),"SELECT password FROM utilizatori WHERE username='$username'");

答案 2 :(得分:0)

因为mysqli_query期望参数1是连接对象。

替换

$password_in_db= mysqli_query("SELECT password FROM utilizatori WHERE username='$username'")

使用

$conn = new Database;
$password_in_db= mysqli_query($conn->dbConnection(),"SELECT password FROM utilizatori WHERE username='".$username."'")

OR

$password_in_db= mysql_query("SELECT password FROM utilizatori WHERE username='".$username."'")

答案 3 :(得分:0)

因为,您正在使用PDO进行数据库连接,为什么使用mysqli_来执行数据库查询。了解有关PDO的更多信息

http://php.net/manual/en/book.pdo.php

关于如何使用它执行数据库查询。或者学习如何使用mysqli进行数据库连接

http://php.net/manual/en/book.mysqli.php

如果您需要执行mysqli_query,您需要在第一个参数中添加数据库连接。

mysqli_query($dblink, "SELECT * FROM City")

答案 4 :(得分:0)

您的重写代码应该是这样的

if(isset($_POST['btn-newpass']))
{
$username = strip_tags($_POST['username']);
$password = md5(strip_tags($_POST['password']));
$password_new = md5(strip_tags($_POST['password_new']));
$password_new_conf = md5(strip_tags($_POST['password_new_conf']));
$password_in_db= mysql_query("SELECT password FROM utilizatori WHERE username='".$username."'")

if(!$password_in_db)
{ echo "The entered username doesn't exist";}
elseif($password!=$password_in_db)
{ echo "The current password is wrong";}

if($password_new == $password_new_conf)
{$sql = mysqli_query("UPDATE utilizatori SET password='$password_new' WHERE username='$username'");}

if($sql)
{ echo "Changed successfully!";}
 else
{ echo "The passwords do not match";}
}

您可以在此链接中了解有关PDO的更多信息

http://php.net/manual/en/book.pdo.php