Table 使用 class 方法时未创建

Table not created when using class method

您好,我创建了以下有效的方法。我对此很陌生,不明白为什么第二个代码不起作用。非常欢迎任何建议。

class Dbcon {
    private $severname;
    private $username;
    private $password;
    private $dbname;
    private $charset;
 
 
public function connect () {
 
$this->servername ='localhost';
$this->username = 'ucmsadmin';
$this->password = 'P@xxwxrdxxx';
$this->dbname = 'umcs';
$this->charset = 'utmf8mb4';
 
 
 try {
 
 
$dsn = "mysql:host=".$this->severname.";dbname=".$this->dbname.";charset".$this->charset;
$pdo = new PDO($dsn , $this->username, $this->password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION);
echo "Connected";
 
 
$queryCreateUsersTable = "CREATE TABLE IF NOT EXISTS `USERS` (
    `ID` int(11) unsigned NOT NULL auto_increment,
    `USER_NAME` varchar(255) NOT NULL default '',
    `EMAIL` varchar(255) NOT NULL default '',
    `PASSWORD` varchar(255) NOT NULL default '',
    `ACCESS_LEVEL` tinyint(1) unsigned NOT NULL default '1',
    `ACCOUNT_APPROVED` boolean NOT NULL default '0',
    
    PRIMARY KEY  (`ID`))";
 
if(!$pdo->query($queryCreateUsersTable)){
    echo "Table creation failed: (" . $pdo->errno . ") " . $pdo->error;
 
} else {
    echo' table created';
}
 
return $pdo;
 
 } catch (PDOException $e) {
 echo "connection failed: ".$e->getMessage();
 
 }
}
}

但是,如果我将创建数据库 table 的部分移动到 public 函数中,我不会收到任何错误,但不会创建 table。这是我所做的;

class Dbcon {
    private $severname;
    private $username;
    private $password;
    private $dbname;
    private $charset;
 
 
public function connect () {
 
$this->servername ='localhost';
$this->username = 'ucmsadmin';
$this->password = 'P@33w0rd101';
$this->dbname = 'umcs';
$this->charset = 'utmf8mb4';
 
 
 try {
 
 
$dsn = "mysql:host=".$this->severname.";dbname=".$this->dbname.";charset".$this->charset;
$pdo = new PDO($dsn , $this->username, $this->password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION);
echo "Connected";
 
 
return $pdo;
 
 } catch (PDOException $e) {
 echo "connection failed: ".$e->getMessage();
 
 }
}
 
public function createtable() {
    $this->connect();
    $queryCreateUsersTable = "CREATE TABLE IF NOT EXISTS `USERS` (
    `ID` int(11) unsigned NOT NULL auto_increment,
    `USER_NAME` varchar(255) NOT NULL default '',
    `EMAIL` varchar(255) NOT NULL default '',
    `PASSWORD` varchar(255) NOT NULL default '',
    `ACCESS_LEVEL` tinyint(1) unsigned NOT NULL default '1',
    `ACCOUNT_APPROVED` boolean NOT NULL default '0',
    
    PRIMARY KEY  (`ID`))";
    return $queryCreateUsersTable;
 
}
}

在index.php我运行

include('classes/dbclass.php');
$object = new Dbcon;
$object->connect();
$object2 = new Dbcon;
$object2->createTable();?> 

createTable()函数只是定义了一个包含一些SQL的字符串。它从不将 SQL 发送到要执行的数据库。

在原代码的基础上,可以这样写:

public function createtable() {
    $pdo = $this->connect();
    $queryCreateUsersTable = "CREATE TABLE IF NOT EXISTS `USERS` (
    `ID` int(11) unsigned NOT NULL auto_increment,
    `USER_NAME` varchar(255) NOT NULL default '',
    `EMAIL` varchar(255) NOT NULL default '',
    `PASSWORD` varchar(255) NOT NULL default '',
    `ACCESS_LEVEL` tinyint(1) unsigned NOT NULL default '1',
    `ACCOUNT_APPROVED` boolean NOT NULL default '0',
    PRIMARY KEY  (`ID`))";

    if(!$pdo->query($queryCreateUsersTable)){
      echo "Table creation failed: (" . $pdo->errno . ") " . $pdo->error;
    } else {
      echo' table created';
    }
}

但是我要指出,每次 运行 查询时都创建一个新连接是非常低效的。相反,您应该尝试在 PHP 脚本的生命周期内创建一次连接,并继续重复使用它。