找回密碼
 註冊
搜索
查看: 276|回復: 0

[教學] PHP 8+MariaDB支援(construct)建構子?CPANEL CentOS 7.9 升級 AlamLinux 9

[複製鏈接]
發表於 2024-12-23 21:32:26 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
我有一個PHP 7(cpanel centos 7.9)專案正在遷移到 PHP 版本 8(alamlinux 9),但 PHP 8版本不支援舊的寫法如果使用建構函式名稱? 搞了構造函數  __construcotr 不少時間

範例如下:
  1. class mysql {
  2.   private $db_host; //數據庫主機
  3.   private $db_user; //數據庫用戶名
  4.   private $db_pwd; //數據庫用戶名密碼
  5.   private $db_database; //數據庫名
  6.   private $conn; //數據庫連接標識;
  7.   private $result; //執行query命令的結果資源標識
  8.   private $sql; //sql執行語句
  9.   private $row; //返回的條目數
  10.   private $coding; //數據庫編碼,GBK,UTF8,gb2312

  11.   /*構造函數*/
  12.   public function __construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding) {
  13.     $this->db_host = $db_host;
  14.     $this->db_user = $db_user;
  15.     $this->db_pwd = $db_pwd;
  16.     $this->db_database = $db_database;
  17.     $this->conn = $conn;
  18.     $this->coding = $coding;
  19.     $this->connect();
  20.   }
  21.   /*數據庫連接*/
  22.   public function connect() {
  23.     if ($this->conn == "pconn") {
  24.     //永久鏈接
  25.       $this->conn = mysqli_pconnect($this->db_host, $this->db_user, $this->db_pwd);
  26.     } else {
  27.     //即使鏈接
  28.     $this->conn = mysqli_connect($this->db_host, $this->db_user, $this->db_pwd);
  29.    }
  30.    mysqli_select_db( $this->conn, $this->db_database ) or die('Error: '.mysqli_error($this->conn).PHP_EOL);

  31.   $this->conn->set_charset("utf8");
  32.   }
  33.   /*數據庫執行語句,可執行查詢添加修改刪除等任何sql語句*/
  34.   public function query($sql) {
  35.     if ($sql == "") {
  36.       $this->show_error("SQL語句錯誤:", "SQL查詢語句為空");
  37.     }
  38.     $this->sql = $sql;
  39.     $result = mysqli_query($this->conn, $this->sql);
  40.                
  41.     if (!$result) {
  42.     //調試中使用,sql語句出錯時會自動打印出來
  43.       if ($this->show_error) {
  44.         $this->show_error("錯誤SQL語句:", $this->sql);
  45.       }
  46.     } else {
  47.       $this->result = $result;
  48.     }
  49.     return $this->result;
  50.   }
  51.   public function fetch_array() {
  52.         return mysqli_fetch_array($this->result, MYSQLI_ASSOC);
  53.   }
  54. }
  55. $db = new mysql($mydbhost, $mydbuser, $mydbpw, $mydbname, "ALL_PS", $mydbcharset);

  56. $sql="SELECT * FROM table1 ORDER BY id ASC";
  57. $query=$db->query($sql);
  58. $row1=$db->fetch_array($query);
  59. $sql="SELECT * FROM table2 where theme_id='".$row1['col1']."'";
  60. $query=$db->query($sql);
  61. $row2=$db->fetch_array($query);
複製代碼




需注意這段
  1. new mysql($mydbhost, $mydbuser, $mydbpw, $mydbname, "ALL_PS", $mydbcharset);
複製代碼
ALL_PS 必須加 單引號或雙引號 ""

及這段
  1. $sql="SELECT * FROM table2 where theme_id='".$row1['col1']."'";
複製代碼


$row1['col1']

col裡面也需 加單引號或雙引號 ""


不然PHP 8 + MariaDB 網頁會反白


示範網站: https://hc-dt.com

PHP MariaDB CPANEL CentOS AlamLinux

PHP  MariaDB CPANEL CentOS  AlamLinux



參考文章
https://stackoverflow.com/questi ... -same-name-as-class

 
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|小黑屋|TShopping

GMT+8, 2025-4-30 17:59 , Processed in 0.027669 second(s), 24 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回復 返回頂部 返回列表