在我的课堂上使用pdo php
我在课堂上使用pdo php在我的课堂上使用pdo php,php,pdo,Php,Pdo,我在课堂上使用pdo php <?php class User { private $db; public function __construct($host, $user, $pass, $db) { try { $this->db = new PDO("mysql:host=$host;dbname=$db", $user, $pass); } catch (Exception $e) {
<?php
class User
{
private $db;
public function __construct($host, $user, $pass, $db)
{
try
{
$this->db = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
}
catch (Exception $e)
{
die('Error : ' . $e->getMessage());
}
}
public function modUser($uid, $email)
{
$query = $this->db->exec("UPDATE Users SET Email =:email WHERE Id_User =:uid");
$query->bindValue(':email', $email, PDO::PARAM_STR);
}
}
?>
它工作得很好,但问题是当使用配置文件连接数据库并写入时
$user = new User($host, $user, $pass, $db);
我得到一个错误:
<b>Warning</b>: PDO::__construct() expects parameter 2 to be string, object given in <b>C:\wamp\www\new_template-latest\new_template\classes\class.User.php</b> on line <b>13</b><br />
<br />
<b>Fatal error</b>: Call to a member function exec() on a non-object in <b>C:\wamp\www\new_template-latest\new_template\classes\class.User.php</b> on line <b>140</b><br />
警告:PDO::\uuu construct()希望参数2是字符串,对象在第13行的C:\wamp\www\new\u template-latest\new\u template\classes\class.User.php中给出
致命错误:在C:\wamp\www\new\u template-latest\new\u template\classes\class.User.php的第140行对非对象调用成员函数exec()
您正在尝试将新用户
对象分配给PDO参数。您需要为用户
对象选择一个新变量名,或为$User
数据库配置变量选择一个新名称。您将$User
变量直接传递到构造函数中,该构造函数现在是一个新用户
对象。您应该将用户名字符串存储在一个具有不同名称的变量中。是否可以包含配置文件?PHP可能正在从配置文件加载它,并将其输入为整数而不是字符串。从他的代码中可以看出,在使用配置文件时,$User
中存储了一个新用户
对象,如果您添加以下行,输出是什么<代码>变量转储($pass);退出代码>确切地说,问题在于变量名,我更改了变量名。这很有效。谢谢大家
<b>Warning</b>: PDO::__construct() expects parameter 2 to be string, object given in <b>C:\wamp\www\new_template-latest\new_template\classes\class.User.php</b> on line <b>13</b><br />
<br />
<b>Fatal error</b>: Call to a member function exec() on a non-object in <b>C:\wamp\www\new_template-latest\new_template\classes\class.User.php</b> on line <b>140</b><br />