2016-10-12 4 views
0

私はちょうど私のプロジェクトフォームをmysqlからmysqliに切り替えることを始めました。だから、私がやったことは、私はちょうど私がオブジェクトを作成し、それオブジェクトを使用したMysqli接続

を使用し、再できクラスデシベルと機能connect.howを作成することですこれは私のコードです:

クラスDB:

class db { 

    //global var session 
    private $biz; 

    //constructor 
    function db (&$b) { 
     $this->biz = $b;    
     $this->connect(); 
    } 

    function connect() { 
     mysqli_connect(
       $this->biz->_db['host'] 
      , $this->biz->_db['username'] 
      , $this->biz->_db['password'] 
         ,$this->biz->_db['database'] 
     ) or die("Unable to connect to database"); 

     //$cont=mysqli_select_db($con,$this->biz->_db['database']) or die("Unable to select database: {$this->biz->_db['database']}"); 
     //return $connt; 
    }  

    /* 
    This function will perform a simple query and return all the results 
    @return  object  Object containing the rows, num_rows and result 

    @param string $sql  The SQL query 
    */ 
    function query($sql) { 

      $result = new fetchQuery($sql); 
      return $result; 
    } 

    /* 
    This function will perform a query given the sql 
    @return  object  Object containing num rows affected and result 

    @param string $sql  The SQL query 
    */ 
    function updateQuery($sql) { 
     return new updateQuery($sql); 
    } 

    /* 
    This function will perform a query given the sql 
    @return  object  Object containing num rows affected and result 

    @param string $sql  The SQL query 
    */ 
    function insertQuery($sql) {  
     return new insertQuery($sql); 
    } 

    /* 
    This function will perform a query given the sql 
    @return  object  Object containing num rows affected and result 

    @param string $sql  The SQL query 
    */ 
    function deleteQuery($sql) { 
     return new deleteQuery($sql); 
    } 

    /* 
    This function will automatically decide if data is being updated or inserted 

    @param array $data  The post object, key/value pairs , already validated 
    @param string $table  The table to be updated 
    */  

    /* 
    This function will perform an update query from the post data that has the correct form - fields matching table field names 
    @return  object  Object containing num rows affected and result 

    @param array $data  The post object, key/value pairs , already validated 
    @param string $table  The table to be updated 
    */ 
    function autoUpdate($data, $table,$wheredata) { 

     //id is required, return false if not found 
     if (!isset($wheredata)) 
     { 
      return false; 
     } 

     $sql = " 
      UPDATE 
       `$table` 
      SET 
     "; 

     foreach ($data as $key => $value) {   
       $sql .= " `$key` = '$value' ,";  
     } 

     //remove extra comma 
     $sql = substr($sql, 0, (strlen($sql) - 1));   
     $sql .= " WHERE 0=0 "; 

     foreach ($wheredata as $key => $value) { 
     $sql .= " and `$key` = '$value' "; 
     } 

     return new updateQuery($sql); 
    } 

    /* 
    This function will perform an insert query from the matching form table 
    @return  object  Object containing num rows affected and result 

    @param array $data  The post object, key/value pairs , already validated 
    @param string $table  The table to be updated 
    */ 
    function autoInsert($data, $table, $validate = false) 
    { 
     $_fields = array(); 
     $_values = array(); 

     foreach ($data as $field => $value) { 
      $_fields[] = $field; 
      $_values[] = $value; 
     } 

     $sql = " 
      INSERT INTO 
       `$table` 
        (
     "; 

     foreach($_fields as $field) { 
      $sql .= "`$field` ,"; 
     } 

     //remove extra comma 
     $sql = substr($sql, 0, (strlen($sql) - 1)); 

     $sql .= " 
      ) VALUES (
     "; 

     foreach($_values as $value) { 
      $sql .= "'$value' ,"; 
     } 

     //remove extra comma 
     $sql = substr($sql, 0, (strlen($sql) - 1)); 

     $sql .= " 
      ) 
     "; 

     return new insertQuery($sql); 
    } 

    /* 
    This function will perform an auto delete query from the matching form table 
    @return  object  Object containing num rows affected and result 

    @param int  $id   The related id 
    @param string $table  The table to be affected 
    */ 
    function autoDelete($sql) {   
     return new deleteQuery($sql);  } 
} 

クエリをフェッチ:

class fetchQuery { 

    public $result; 
    public $num = 0; 
    public $rows = array(); 
    public $error = false; 

    function fetchQuery($sql) { 

     $this->result = mysql_query($sql);  

     if ($this->result) { 
      $this->num = mysql_num_rows($this->result); 
      if ($this->num) { 
       while($row = mysql_fetch_assoc($this->result)) { 
        $this->rows[] = $row; 
       } 
      } else { 
       $this->result = false; 
      } 
     } else { 
      $this->result = false; 
      $this->error = mysql_error(); 
     } 
    } 

私に接続を使用して作成フェッチクエリの解決策を与えてください 関数。

+1

をhttp://php.net/manual/en/language.oop5.basic.php – RST

+0

まず第一に、今、あなたはmysqliのとMySQLの機能を混ぜます。第二に、私はこれが良いデザインではないと思う。何が起こっていてストアしているデータベースクラスを1つ作成してみませんか? – vaso123

+0

mysqliのようにmysqli_query($ con、$ sql)に変更したいと思います.my疑問はどこから$ conを作成するのか、fetchQuery関数にアクセスする方法です –

答えて

1

は、以下のような構造を作成し、

データベース設定を試してみてください。

<?php 
$glob['dbhost'] = 'localhost'; 
$glob['dbusername'] = 'username'; 
$glob['dbpassword'] = 'password'; 
$glob['dbdatabase'] = 'database'; 
?> 

Databaseクラスdbconfig.php:database.class.php

<?php 

class database { 

var $_sql   = ''; 
/** @var Internal variable to hold the connector resource */ 
var $_resource  = ''; 
/** @var Internal variable to hold the query result*/ 
var $_result  = ''; 

/** 
* Database object constructor 
* @param string Database host 
* @param string Database user name 
* @param string Database user password 
* @param string Database name 
*/ 
function database() { 
    global $glob; 
    $host = $glob['dbhost']; 
    $user = $glob['dbusername']; 
    $pass = $glob['dbpassword']; 
    $db = $glob['dbdatabase']; 
    $this->_resource = @mysqli_connect($host, $user, $pass, $db); 
    if (mysqli_connect_errno()){ 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
    else{ 
     echo "Could not connect to the database!"; 
     exit; 
    }    
} 

/** 
* Execute the query 
* @return mixed A database resource if successful, FALSE if not. 
*/ 
function query($sql) { 
    $_sql = $sql; 
    return $_result = @mysqli_query($this->_resource, $_sql);    
} 

function fetchArray($result) { 
    return @mysqli_fetch_array($result); 
} 

function fetchAssoc($result) { 
    return @mysqli_fetch_assoc($result); 
} 
} 
?> 

プロジェクトの設定:configuration.php

<?php 
session_start(); 
ob_start("ob_gzhandler"); 
@ob_gzhandler(); 
error_reporting(E_ERROR); 

require_once('dbconfig.php'); 

require_once('database.class.php'); 
$dbclass = new database(); 
?> 

任意のプロジェクトファイルのコード:

<?php 
require('configuration.php'); 

$sqltepm="SELECT * FROM tblname"; 
$restepm=$dbclass->query($sqltepm); 
while($row=$dbclass->fetchArray($restepm)){ 
    extract($row); 
} 
?> 
1

戦略のような基本機能を作成して、このタイプのシナリオを処理できます。したがって、関数ファイルだけを含めると、DBは即座に接続され、DBに接続する変数を使用してこれを使用できます。

2つのファイルを持つことができ、このようなシナリオを実行できます。

<?php 
class DB { 
    function __construct(){ 
     $username='root'; 
     $password=''; 
     $host='localhost'; 
     $db='store'; 
     $this->connection = mysqli_connect($username,$password,$host,$db); 
     if(mysqli_connect_errno){ 
      echo "Failed to connect to MYSQL: " . mysqli_connect_error(); 
     } 
    } 

    function fetchData(){ 
     $get_query = "SELECT * FROM TABLE" 
     $result = mysqli_query($this->connection, $get_query); 
    } 

} 
?> 

をdb.php

そして、次のようにページ内の関数を呼び出すことができます。私たちが作成したクラスの

使用

私は、コードの上に私の説明が明確であり、あなたはそれをよく理解し、継続することができそう願っ

<?php 
include('db.php'); // Include the DB file over this line 
$conn= new DB(); // Initialize the class that we have created 
$conn->fetchData();// This query will select all the data and return it. 
?> 

listpage.phpあなたのプロジェクトコードはオンザフライで表示されます。

ハッピーコーディング:)

関連する問題