2017-05-16 196 views
0

我正在瀏覽此代碼,無法完全破解它。請幫助,我想要在兩個文件之間獲得數據庫連接,並在第一個代碼上出現錯誤,並以//連接到客戶端數據庫開始,並且似乎無法弄清楚,我嘗試了多種不同的方法關於這個問題。數據庫連接問題

這是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(); 
} 


?> 

回答

0

不幸的是,包括不喜歡的工作,你需要實際使用的變量本身:

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

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

此外,不需要在中創建MySQLi連接3210,然後再次在你的類構造函數中。通過在k_db.php中創建它,然後將其包含在您的課程中,這樣的連接在$mysqli_connect已存在於相同的範圍內。

使用$data = include('/file/script.php')的唯一時間對於獲取自定義數據是有意義的,即當您包含的腳本使用return時。見文件:http://php.net/manual/en/function.include.php