2

I use pdo php in my class

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

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 />
3
  • Can you please include the config file? PHP may be loading it from the config file and typing it as an integer instead of a string. Commented Jul 8, 2011 at 15:10
  • It appears from his code that a new User object is stored in $user Commented Jul 8, 2011 at 15:10
  • When using the config file, if you add the following line what is output? var_dump($pass); exit; Commented Jul 8, 2011 at 15:11

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly the problem is in variables name ,i change variable name it's work .thank you for all
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.