|
我有一個PHP 7(cpanel centos 7.9)專案正在遷移到 PHP 版本 8(alamlinux 9),但 PHP 8版本不支援舊的寫法如果使用建構函式名稱? 搞了構造函數 __construcotr 不少時間
範例如下:
- class mysql {
- private $db_host; //數據庫主機
- private $db_user; //數據庫用戶名
- private $db_pwd; //數據庫用戶名密碼
- private $db_database; //數據庫名
- private $conn; //數據庫連接標識;
- private $result; //執行query命令的結果資源標識
- private $sql; //sql執行語句
- private $row; //返回的條目數
- private $coding; //數據庫編碼,GBK,UTF8,gb2312
- /*構造函數*/
- public function __construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding) {
- $this->db_host = $db_host;
- $this->db_user = $db_user;
- $this->db_pwd = $db_pwd;
- $this->db_database = $db_database;
- $this->conn = $conn;
- $this->coding = $coding;
- $this->connect();
- }
- /*數據庫連接*/
- public function connect() {
- if ($this->conn == "pconn") {
- //永久鏈接
- $this->conn = mysqli_pconnect($this->db_host, $this->db_user, $this->db_pwd);
- } else {
- //即使鏈接
- $this->conn = mysqli_connect($this->db_host, $this->db_user, $this->db_pwd);
- }
- mysqli_select_db( $this->conn, $this->db_database ) or die('Error: '.mysqli_error($this->conn).PHP_EOL);
- $this->conn->set_charset("utf8");
- }
- /*數據庫執行語句,可執行查詢添加修改刪除等任何sql語句*/
- public function query($sql) {
- if ($sql == "") {
- $this->show_error("SQL語句錯誤:", "SQL查詢語句為空");
- }
- $this->sql = $sql;
- $result = mysqli_query($this->conn, $this->sql);
-
- if (!$result) {
- //調試中使用,sql語句出錯時會自動打印出來
- if ($this->show_error) {
- $this->show_error("錯誤SQL語句:", $this->sql);
- }
- } else {
- $this->result = $result;
- }
- return $this->result;
- }
- public function fetch_array() {
- return mysqli_fetch_array($this->result, MYSQLI_ASSOC);
- }
- }
- $db = new mysql($mydbhost, $mydbuser, $mydbpw, $mydbname, "ALL_PS", $mydbcharset);
- $sql="SELECT * FROM table1 ORDER BY id ASC";
- $query=$db->query($sql);
- $row1=$db->fetch_array($query);
- $sql="SELECT * FROM table2 where theme_id='".$row1['col1']."'";
- $query=$db->query($sql);
- $row2=$db->fetch_array($query);
複製代碼
需注意這段
- new mysql($mydbhost, $mydbuser, $mydbpw, $mydbname, "ALL_PS", $mydbcharset);
複製代碼 ALL_PS 必須加 單引號或雙引號 ""
及這段
- $sql="SELECT * FROM table2 where theme_id='".$row1['col1']."'";
複製代碼
$row1['col1']
col裡面也需 加單引號或雙引號 ""
不然PHP 8 + MariaDB 網頁會反白
示範網站: https://hc-dt.com
PHP MariaDB CPANEL CentOS AlamLinux
參考文章
https://stackoverflow.com/questi ... -same-name-as-class
|
PHP, MariaDB, CPANEL, CentOS, AlamLinux, CPANEL, CentOS, AlamLinux, CPANEL, CentOS, AlamLinux
|