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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘