douyi6960 2017-10-31 05:51
浏览 35
已采纳

在PHP类中包含MySQL配置文件[重复]

This question already has an answer here:

I want to include MySQL config file inside PHP class.

<?php
class User {
    private $dbHost     = "localhost";
    private $dbUsername = "root";
    private $dbPassword = "";
    private $dbName     = "gic";
    private $userTbl    = 'users';

    function __construct(){
        if(!isset($this->db)){
            // Connect to the database
            $conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);
            if($conn->connect_error){
                die("Failed to connect with MySQL: " . $conn->connect_error);
            }else{
                $this->db = $conn;
            }
        }
    }

    function checkUser($userData = array()){
        if(!empty($userData)){

            $host  = $_SERVER['HTTP_HOST'];
            $host_upper = strtoupper($host);
            $path   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
            $baseurl = "http://" . $host . $path ."/";


            $prevQuery = "SELECT * FROM ".$this->userTbl." WHERE oauth_provider = '".$userData['oauth_provider']."' AND oauth_uid = '".$userData['oauth_uid']."'";
            $prevResult = $this->db->query($prevQuery);

           <!-- rest of my code --> 
        }
    }
}

When I run this file, it's working fine, but I want to separate the MySQL database connection to another file (config.php), so I edited my code:

<?php
class User {

include 'config.php';

$connection = new createCon();
$conn = $connection->connect();

function checkUser($userData = array()){
    if(!empty($userData)){

        $host  = $_SERVER['HTTP_HOST'];
        $host_upper = strtoupper($host);
        $path   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        $baseurl = "http://" . $host . $path ."/";


        $prevQuery = "SELECT * FROM ".$this->userTbl." WHERE oauth_provider = '".$userData['oauth_provider']."' AND oauth_uid = '".$userData['oauth_uid']."'";
        $prevResult = $conn->query($prevQuery);
        }
    }
}

and config.php

<?php

class createCon  {
    var $host = 'localhost';
    var $user = 'root';
    var $pass = '';
    var $db = 'gic';
    var $myconn;

    function connect() {
        $con = mysqli_connect($this->host, $this->user, $this->pass, $this->db);
        if (!$con) {
            die('Could not connect to database!');
        } else {
            $this->myconn = $con;
            echo 'Connection established!';}
        return $this->myconn;
    }

    function close() {
        mysqli_close($myconn);
        echo 'Connection closed!';
    }
}
?>

When I run the code above, it's showing an error:

Parse error: syntax error, unexpected 'include' (T_INCLUDE), expecting function (T_FUNCTION) or const (T_CONST) in C:\xampp\htdocs\gic001\User.php on line 4

Any idea?

</div>
  • 写回答

3条回答 默认 最新

  • doubi7346 2017-10-31 06:57
    关注

    With inheritance, it should not be a big problem. Keep your config file as it is and try this.

    include_once 'config.php';
    
    class User extends createCon {
    
        private $conn;
    
        function __construct(){
            $this->conn = $this->connect();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等