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