Using pdo php in my class
I use pdo php in my class
<?php
class User
{
private $db;
public f开发者_运维知识库unction __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);
}
}
?>
When i use the class user like this:
$user = new User('localhost','tester','0000','Agency);
It works well,but the problem is when use config file to connect database and write
$user = new User($host, $user, $pass, $db);
I get an error:
<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 />
You are trying to assign the new User
object to the PDO parameters. You need to choose a new variable name for your User
object or a new name for your $user
database configuration variable.
You are passing the $user
variable straight into the constructor, which is now a new User
object. You should store the username string in a variable with a different name.
精彩评论