私はちょうど私のプロジェクトフォームを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();
}
}
私に接続を使用して作成フェッチクエリの解決策を与えてください 関数。
をhttp://php.net/manual/en/language.oop5.basic.php – RST
まず第一に、今、あなたはmysqliのとMySQLの機能を混ぜます。第二に、私はこれが良いデザインではないと思う。何が起こっていてストアしているデータベースクラスを1つ作成してみませんか? – vaso123
mysqliのようにmysqli_query($ con、$ sql)に変更したいと思います.my疑問はどこから$ conを作成するのか、fetchQuery関数にアクセスする方法です –