2017-05-16 5 views
0

Ich gehe durch diesen Code und kann es nicht wirklich knacken. Bitte helfen Sie mir, ich versuche eine Datenbankverbindung zwischen zwei Dateien zu bekommen und bekomme immer einen Fehler beim ersten Code in Zeilen, die mit // Connect to Client Database beginnen und ich kann es nicht herausfinden. Ich habe mehrere verschiedene Wege ausprobiert über dieses Problem zu gehen.DB Verbindungsproblem

dies ist der k_dbfunctions Code

<?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; 
     } 
    } 
} 

dies der k_db Code

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


?> 

Antwort

0

Leider funktioniert nicht wie das einschließlich, die Variablen selbst verwenden müssen, um tatsächlich ist:

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

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

Es ist auch nicht notwendig, die MySQLi-Verbindung inzu erstellenund dann wieder in Ihrem Klassenkonstruktor. Durch Erstellen in k_db.php dann einschließlich Ihrer Klasse, existiert eine solche Verbindung bereits in $mysqli_connect innerhalb des gleichen Umfangs.

Die einzige Zeit, die $data = include('/file/script.php') verwendet, ist für das Abrufen benutzerdefinierter Daten sinnvoll, wenn das von Ihnen eingeschlossene Skript return verwendet. Siehe die Dokumentation: http://php.net/manual/en/function.include.php