2016-03-29 18 views
-1

boş hata) (hazırlamak:alma Çağrı bu hatayı hazırlanmış deyimi çalıştırmak için bir veritabanı nesnesini kullanarak ancak alıyorum

kullanıyorum veritabanı nesnesidir Fatal error: Call to a member function prepare() on null in /var/www/html/rsvp/lib/classes.php on line 43

(bu sadece bunun bir parçası, ama içeren tüm ilgili yöntemleri):

class DatabaseConnection { 
    private $host = DB_HOST; 
    private $user = DB_USER; 
    private $pass = DB_PASS; 
    private $dbname = DB_NAME; 
    protected $dbConnect; 
    private $stmt = NULL; 
    private $result; 

    public function __construct() { 
    // Set DSN 
    $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; 
    // Set options 
    $options = array(
     PDO::ATTR_PERSISTENT  => true, 
     PDO::ATTR_ERRMODE   => PDO::ERRMODE_WARNING, 
     PDO::ATTR_EMULATE_PREPARES => false 
    ); 

    // Create a new PDO instanace 
    try{ 
     $this->dbConnect = new PDO($dsn, $this->user, $this->pass, $options); 
    } 
    // Catch any errors 
    catch(PDOException $e) { 
     $this->error = $e->getMessage(); 
    } 
    } 

    //Prepare statement 
    public function preparedQuery($query) { 
    //Unset previous stmt 
    unset($this->stmt); 
    //Set up new prepared statment 
    $this->stmt = $this->dbConnect->prepare($query); 
    } 

    //Bind paramaters 
    public function bind($param, $value, $type = null) { 
    if (is_null($type)) { 
     switch (true) { 
      case is_int($value): 
       $type = PDO::PARAM_INT; 
       break; 
      case is_bool($value): 
       $type = PDO::PARAM_BOOL; 
       break; 
      case is_null($value): 
       $type = PDO::PARAM_NULL; 
       break; 
      default: 
       $type = PDO::PARAM_STR; 
     } 
    } 
    $this->stmt->bindValue($param, $value, $type); 
    } 

    //Execute statement 
    public function execute() { 
    return $this->stmt->execute(); 
    } 

Ve sorguyu çalıştırmak için kullanıyorum yöntemidir:

class SMS extends DatabaseConnection { 

    public function __construct() { 

    } 

    public function createSMSSession($phoneNumber) { 
    //Add to sms table 
    $this->preparedQuery("INSERT INTO sms (phone_number, step) VALUES (:phonenumber, :step)"); 
    $this->bind(':phonenumber', $phoneNumber); 
    $this->bind(':step', 1); 
    $this->execute();  
    } 
} 
.210

Ve son olarak, ben yöntemini çağırmak için kullanıyorum kodu:

require_once('lib/config.php'); 
require_once('lib/classes.php'); 

// Sender's phone numer 
$from_number = $_REQUEST['From']; 
// Receiver's phone number - Plivo number 
$to_number = $_REQUEST["To"]; 
// The SMS text message which was received 
$text = $_REQUEST["Text"]; 

$sms = new SMS(); 
$sms->createSMSSession($from_number); 

veritabanı kimlik bilgileri config.php de tanımlanır. Her şeyin bu noktada doğru olduğunu doğruladım. Aynı veritabanı nesnesini hatasız kullanarak birden çok yönteme sahibim.

+1

'SMS' sınıfınızın' DatabaseConnection 'örneğiyle nasıl bir ilişki kurduğunu göstermediniz. Bir bağlantı kesme işleviniz varsa veya sapı bozan bir şey varsa, alıntı da yeterli olmaz. – mario

+0

SMS sınıfı, Veritabanı Bağlantısını genişletir. Sadece boş bir __construct ve gönderdiğim yöntemi içerir. Bütün sınıfı göstermek için düzenleyeceğim. –

+0

'$ dsn = 'mysql: host =' içinde' user'/'password' eksiksiniz gibi görünüyor. $ this-> host. '; dbname ='. $ this-> dbname; ' – Sean

cevap

0

Aslında orada bir şey yapmıyorsanız, tüm SMS::__constuct bildirimini kaldırın. DatabaseConnection::__construct işlevini çağırmazsa, bir PDO tanıtıcınız olmaz.

+0

Teşekkür ederiz, teşekkürler. Ben SMS sınıfının __construct 'DatabaseConnection :: __ construct();' koymak ve çalışıyor. –

İlgili konular