数据库连接问题

时间:2017-05-16 01:19:00

标签: php database mysqli

我正在浏览此代码并且无法解决问题。请帮忙,我要在两个文件之间建立数据库连接,并在第一个代码的第一个代码上出现错误,这些代码以//连接到客户端数据库开头,并且似乎无法弄清楚,我&#39我们尝试了多种不同的方式来解决这个问题。

这是k_dbfunctions代码

<?php
//session_start();

/**
 *  PDO Class
 */ 
class PDOData {

    private $dbhost;
    private $dbuser;
    private $bdpass;
    private $dbnamee;
    private $connection = null;

    /**
     *  DBO Class Constructor
     *  Get config file data and assign into instance variables.
     *
     *  Get Database Connection
     *  Default connect to admin database.
     *  @return connection if connection is successful.
     */
    function __construct() {
        // Get database credentials from config file.
        $path = "k_db.php";
        $data = include($path);

        if(is_array($data)) {
            $this->hostName   = $data['hostinfo goes here'];
            $this->userName   = $data['db name goes here'];
            $this->password   = $data['password goes here'];
            $this->dbName     = $data['db name goes here'];
        }

        // Connect to Client Database
        if(isset($_SESSION['admin']['b00635911'])) {
            $this->dbName = $_SESSION['admin']['b00635911'];
        }

        // Make Connection
        if($this->connection==null){
            $this->connection = mysqli_connect($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);

            // Check Connection
            if (mysqli_connect_errno()){
                echo "Failed to connect to MySQL: " . mysqli_connect_error();die();
            }
        }
    }

    /**
     *  Check Authentication
     *  Check record is found in table or not.
     *  If found it will return true otherwise return false
     *  @return boolean
     */
    function checkAuthentication($query){
        $result = mysqli_fetch_assoc(mysqli_query($this->connection, $query));

        if(isset($result['id']) && !empty($result['id'])){
            return true;
        } else {
            return false;
        }
    }

    /**
     *  Insert record in table.
     *  @return if inserted sucessfully then return last id otherwise return false
     */
    function insetData($query){
        if (mysqli_query($this->connection, $query) === TRUE) {
            return mysqli_insert_id($this->connection);
        }else{
            return false;
        }
    }

    /**
     *  Return all table records.
     *  @return Result Set
     */
    function getData($query){
        return mysqli_query($this->connection, $query);
    }

    /**
     *  Update record
     *  @return boolean
     */
    function updateData($query){
        if (mysqli_query($this->connection, $query) === TRUE) {
            return true;
        }else{
            return false;
        }
    }

    /**
     *  Delete Record
     *  @return boolean
     */
    function deleteData($query){
        if (mysqli_query($this->connection, $query) === TRUE) {
            return true;
        }else{
            return false;
        }
    }
}

这是k_db代码

<?php

//Connection credentials
 $dbhost="db host name here";
 $dbuser="db user here";
 $dbpass="my db password";
 $dbname="my db name";


//Creating mysqli object
$mysqli_connect= new mysqli ($dbhost, $dbuser, $dbpass, $dbname);


//Error handler 
if($mysqli->connect_error){
    printf("Connection failed %\n", $mysqli->connect_error);
    exit();
}


?>

1 个答案:

答案 0 :(得分:0)

不幸的是,包括没有这样的工作,你需要实际使用变量本身:

$path = "k_db.php";
include($path);

$this->hostName   = $dbhost;
$this->userName   = $dbuser;
$this->password   = $dbpass;
$this->dbName     = $dbname;

此外,无需在k_db.php中创建MySQLi连接,然后再在类构造函数中创建。通过在k_db.php中创建然后将其包含在您的类中,此类连接已存在于同一范围内的$mysqli_connect中。

使用$data = include('/file/script.php')的唯一时间对于获取自定义数据有意义,因为您所使用的脚本使用return。请参阅文档:http://php.net/manual/en/function.include.php

相关问题