dongrong6235 2019-07-06 10:23
浏览 189
已采纳

mysql数据库错误:在null上调用成员函数query()

I am trying to create a mysql table using php

I have the following function to create the table

class dbActions{
public $connection;
function dbConnect(){
    $dbname = 'kilimokenya';
    $dbhost = 'localhost';
    $dbpass = '';
    $dbuser = 'root';

    $connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

    if($connection ->connect_errno) echo ($connection ->connect_error());
}

function createCustomerTbl(){
    GLOBAL $connection;
    $CustomerTbl = "CREATE TABLE customers_tbl(
        customer_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        firstname VARCHAR(30) NOT NULL,
        lastname VARCHAR(30) NOT NULL,
        emailAddress VARCHAR(30) NOT NULL,
        phone_number VARCHAR(15) NOT NULL,
        dateRegistered TIMESTAMP
        )";
    $check = $connection ->query($CustomerTbl);
    if(!$check)
        echo "Customer table not created because ".$connection ->error;
    return true;
}

I call the function here :

$dbObject = new dbActions();
$dbObject ->dbconnect();

$dbObject ->createCustomerTbl();

I get the following error when i run the code: Fatal error: Call to a member function query() on null in C:\xampp\htdocs\current\kilimokenyafoods\common\server.php on line 31

What im i missing?

  • 写回答

1条回答 默认 最新

  • dougang1967 2019-07-06 10:50
    关注

    The $connection property should be accessed within the class using $this rather than declaring as a global variable.

    The above could be rewritten a little to follow the singleton pattern to ensure that only one instance of the class is initialised for your scripts

    class dbAction{
    
        private static $instance=false;
        private $connection;
    
        private function __construct($dbhost, $dbuser, $dbpass, $dbname){
            $this->connection=new mysqli($dbhost, $dbuser, $dbpass, $dbname);
        }
        public static function initialise($dbhost, $dbuser, $dbpass, $dbname){
            if( !self::$instance ) self::$instance=new dbAction( $dbhost, $dbuser, $dbpass, $dbname );
            return self::$instance;
        }
    
    
        public function createCustomerTbl(){
            $sql='CREATE TABLE customers_tbl(
                customer_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                firstname VARCHAR(30) NOT NULL,
                lastname VARCHAR(30) NOT NULL,
                emailAddress VARCHAR(30) NOT NULL,
                phone_number VARCHAR(15) NOT NULL,
                dateRegistered TIMESTAMP)';
            $res = $this->connection->query( $sql );
            if( !$res )printf( 'Customer table not created because : %s', $this->connection->error );
            return $res;
        }
    }
    
    
    
    $dbo=dbAction::initialise( 'localhost', 'root', 'xxx', 'kilimokenya' );
    $status=$dbo->createCustomerTbl();
    
    echo $status ? 'good' : 'bad';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?