doukan1258 2016-03-29 03:31
浏览 61
已采纳

在null错误上调用成员函数prepare()

I am using a database object to run a prepared statement but getting this error:

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

The database object I am using is (this is only part of it, but includes all relevant methods):

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

And the method I'm using to run the query is:

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

And, finally, the code I'm using to call the method:

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

The database credentials are defined in the config.php file. I have verified that everything is correct on that end. I have multiple methods using the same Database object with no error.

  • 写回答

1条回答 默认 最新

  • dongxibeng5324 2016-03-29 04:00
    关注

    Remove the whole SMS::__constuct declaration if you're not actually doing anything there. Unless it invokes the DatabaseConnection::__construct function, you won't have a PDO handle.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?