2016-03-29 7 views
-1

私はプリペアドステートメントを実行するために、データベース・オブジェクトを使用しますが、このエラーを取得しています:取得コール)

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

私が使用していたデータベース・オブジェクトがある(これはそれだけの一部が、含まれるすべての関連するメソッド):

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

そして、私は、クエリを実行するために使用している方法は次のとおりです。

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

そして、最後に、私はメソッドを呼び出すために使用しているコードは:

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

データベース資格情報がconfig.phpファイルで定義されています。私はそのことがすべて正しいことを確認しました。同じデータベースオブジェクトをエラーなしで使用する複数のメソッドがあります。

+1

あなたの 'SMS'クラスが' DatabaseConnection'インスタンスにどのように関係しているのかを示していません。あなたがどこかでハンドルを解除する何かを切断する機能を持っているなら、その抜粋だけでは十分ではありません。 – mario

+0

SMSクラスはDatabaseConnectionを拡張します。それは空の__constructと私が投稿したメソッドだけで構成されています。クラス全体を表示するように編集します。 –

+0

'$ dsn = 'mysql:host ='に' user'/'password'がありません。 $ this-> host。 '; dbname ='。 $ this-> dbname; ' – Sean

答えて

0

実際に何もしていない場合は、SMS::__constuct宣言全体を削除してください。 DatabaseConnection::__construct関数を呼び出さない限り、PDOハンドルはありません。

+0

ありがとう、ありがとうございます。私は 'DatabaseConnection :: __ construct();'をSMSクラスの__constructに入れて動作させています。 –

関連する問題