php - fatal error : Call to a member function prepare() on a non-object,调用prepare语句的对象不为null

标签 php mysql

我已经在网上搜索过,但没有找到任何解决我的问题的方法。我有一个类 DB_Functions,它使用准备语句来执行 SQL 查询。 DB_Functions.php

<?php

 class DB_Functions {

 private $con;

 //constructor
 function __construct(){
   require_once __DIR__  . "/db_connect.php";
   //connecting to database
   $db = new DB_CONNECT();
   $this->con = $db->connect();
  / /var_dump($conn);
 }

 //destructor
 function __destruct(){

  }

//store new firm //testing
public function storeFirm($name, $email, $tel, $address, $city, $code){
    var_dump($this->con);
    $stmt = $this->con->prepare("INSERT INTO Charset (charset) VALUES (?)");
    $stmt->bind_param("s", $name);
    $stmt->execute();
    $stmt->close();
 }



 }

 ?>

变量 $this->con 使用 var_dump 返回类型(mysql 链接)的资源(4)

db_connect.php

  class DB_CONNECT{

 function __construct(){
    //import database connection variables
    require_once __DIR__ . "/db_config.php";
  }
 /*
 function to connect with database
 */
function connect(){

    //connecting to mysql database
    $conn = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die (mysql_error() . PHP_EOL);
    //select database
    $db = mysql_select_db(DB_DATABASE, $conn) or die (mysql_error() . PHP_EOL);

    /*check connection*/
    if ( mysql_error() ){
      printf("Connect failed: %s\n", mysql_error());
      exit();
    }
    else {
      mysql_set_charset("utf8", $conn) or die (mysql_error() . PHP_EOL);
      //$this->setCharacterSet($con);
      //returning connection cursor
      //var_dump($conn);
      return $conn;
    }
}

/*function to close db connection*/
function close(){
  //closing db connection
  mysql_close();
}

 //set utf-8 character set
 function setCharacterSet($con){
    if ( mysql_client_encoding($con) != "utf8"){
        //mysql_set_charset("utf8");
        printf("The current character set is : " . mysql_client_encoding($con) . "\n");
    }
 }

  }
?>

谁能告诉我罪魁祸首位于我的代码中的哪里?

最佳答案

php 扩展 mysql 已弃用,我相信它没有准备好的语句,因此您尝试在类中不存在的对象上使用函数。

准备好的语句可在 mysqliPDO 中使用。

关于php - fatal error : Call to a member function prepare() on a non-object,调用prepare语句的对象不为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33197743/

相关文章:

php postgresql pg_fetch_all

php - 在 CodeIgniter 中设置 Session_id

php - Yii2自定义errorHandler组件

窗口提交方法内的Javascript停止工作

MySQL 按分隔符分割字符串的函数不适用于波兰语特殊字符

mysql - 从三个表中选择

php - 如何显示特定名称的计数?并使用 php sql 显示页面?

php - Antergos Linux - LEMP 堆栈 - Nginx 403

mysql - 选择两个日期之间的记录

Mysql 将 unicode 代码点转换为 utf-8 字符