2016-07-07 9 views
-2

Ich bekomme diese Fehlermeldung, wenn eine PDO-Verbindung herzustellen versucht:Objekt kann nicht in String umgewandelt wird

Objekt der Klasse dbConnection konnte nicht in String in (line) umgewandelt wird

Dies ist mein Code:

class dbConnection 
{ 
    protected $db_conn; 
    public $db_name = "todo"; 
    public $db_user = "root"; 
    public $db_pass = ""; 
    public $db_host = "localhost"; 

    function connect() 
    { 
     try { 
      $this->db_conn = new PDO("mysql:host=$this->$db_host;$this->db_name", $this->db_user, $this->db_pass); 
      return $this->db_conn; 
     } 
     catch (PDOException $e) { 
      return $e->getMessage(); 
     } 
    } 
} 

der Fehler auf der PDO Linie ist. Nur für den Fall, füge ich den Code, wo ich die Methode connect() zugreifen:

class ManageUsers 
{ 
    public $link; 

    function __construct() 
    { 
     $db_connection = new dbConnection(); 
     $this->link = $db_connection->connect(); 
     return $link; 
    } 

    function registerUsers($username, $password, $ip, $time, $date) 
    { 
     $query = $this->link->prepare("INSERT INTO users (Username, Password, ip, time1, date1) VALUES (?,?,?,?,?)"); 
     $values = array($username, $password, $ip, $time, $date); 
     $query->execute($values); 
     $counts = $query->rowCount(); 
     return $counts; 
    } 
} 

$users = new ManageUsers(); 
echo $users->registerUsers('bob', 'bob', '127.0.0.1', '16:55', '01/01/2015'); 
+1

Nopes ... Ihre Frage enthält nicht die richtige Zeile. –

+0

Es enthält den ganzen Code, den ich habe ... @PraveenKumar – Adir

+0

Warum geben Sie etwas in einem Konstruktor zurück? – apokryfos

Antwort

1
Ihre Verbindungseinstellung zu folgenden Änderung

:

class dbConnection 
{ 
    protected $db_conn; 
    public $db_name = "todo"; 
    public $db_user = "root"; 
    public $db_pass = ""; 
    public $db_host = "localhost"; 

    function connect() 
    { 
     try { 
      $this->db_conn = new PDO("mysql:host={$this->db_host};{$this->db_name}", $this->db_user, $this->db_pass); //note that $this->$db_host was wrong 
      return $this->db_conn; 
     } 
     catch (PDOException $e) { 
      //handle exception here or throw $e and let PHP handle it 
     } 
    } 
} 

Zusätzlich Rückkehr Wert in einem Konstruktor nicht hat Nebenwirkungen (und sollte strafrechtlich verfolgt werden).

0

Bitte folgen Sie unten Code, es ist auf meinem Server getestet und läuft gut.

class Config 
    { 
    var $host = ''; 
    var $user = ''; 
    var $password = ''; 
    var $database = ''; 

    function Config() 
    { 
     $this->host  = "localhost"; 
     $this->user  = "root"; 
     $this->password = ""; 
     $this->database = "test";  
    } 

} 

function Database() 
{ 
     $config = new Config(); 

     $this->host = $config->host; 
     $this->user = $config->user; 
     $this->password = $config->password; 
     $this->database = $config->database; 
} 

function open() 
{ 

    //Connect to the MySQL server 
    $this->conn = new PDO('mysql:host='.$this->host.';dbname='.$this->database, $this->user,$this->password); 
    if (!$this->conn) 
    { 
     header("Location: error.html"); 
     exit; 
    } 

    return true; 
} 
+0

woher bekommst du die config? – Adir

Verwandte Themen