PHP 面向对象的mysql数据库操作类功能实例

前端之家收集整理的这篇文章主要介绍了PHP 面向对象的mysql数据库操作类功能实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

<?PHP
/**
 * @R_404_198@数据库操作类
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
 **/
class database {

var $host = NULL;
var $username = NULL;
var $password = NULL;
var $databaseName = NULL;
var $link = NULL;
var $queries = NULL;
var $errors = NULL;

var $databaseExtras = NULL;

function __construct($host,$username,$password,$database) {
$this->database($host,$database);
}

function database($host,$database) {
/*$this->database = array (
"host" => $host,"username" => $username,"password" => $password,"database" => $database,"link" => "","queries" => array (),"errors" => array ()
);*/

$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->databaseName = $database;
$this->link = "";
$this->queries = array ();
$this->errors = array ();

$this->databaseExtras = new stdClass;

$this->link = @R_404_198@_connect($this->host,$this->username,$this->password) or die("Could not connect to Database");
@R_404_198@_select_db($this->databaseName);
}

function justquery($sql) {
$this->queries[] = $sql;
return @R_404_198@_query($sql,$this->link);
}

function loadResult($sql) {
if (!($cur = $this->justquery($sql))) {
return null;
}
$ret = null;
if ($row = @R_404_198@_fetch_row( $cur )) {
$ret = $row[0];
}
@R_404_198@_free_result( $cur );
return $ret;
}

function loadFirstRow($sql) {
if (!($cur = $this->justquery($sql))) {
return null;
}
$ret = null;
if ($row = @R_404_198@_fetch_object( $cur )) {
$ret = $row;
}
@R_404_198@_free_result( $cur );
return $ret;
}

function insertid() {
return @R_404_198@_insert_id( $this->link );
}

function query($sql,$key = "",$returns = true,$batch = false) {
$result = array ();

switch ($batch) {
default:
case true:
foreach ($sql as $index => $query) {
$this->queries[] = $query;
$answer = @R_404_198@_query($query,$this->link);

if (!$answer) {
$this->errors[] = @R_404_198@_error($this->link);
}
else {
if ($returns != false) {
if (@R_404_198@_num_rows($answer) > 0){
while ($row = @R_404_198@_fetch_object($answer)) {
if ($key != ""){
$result[$index][$row->$key] = $row;
}
else {
$result[$index][] = $row;
}
}
} else {}
} else {}
}
}
break;

case false:
$this->queries[] = $sql;
$answer = @R_404_198@_query($sql,$this->link);

if (!$answer) {
$this->errors[] = @R_404_198@_error($this->link);
$result = false;
}
else {
if ($returns != false) {
if (@R_404_198@_num_rows($answer) > 0){
while ($row = @R_404_198@_fetch_object($answer)) {
if ($key != ""){
$result[$row->$key] = $row;
}
else {
$result[] = $row;
}
}
} else {}
}
else {
$result = true;
}
}
break;
}

return $result;
}

function loadObject( $sql,&$object ) {
if ($object != null) {
if (!($cur = $this->justquery($sql))) {
return false;
} else {}
if ($array = @R_404_198@_fetch_assoc( $cur )) {
@R_404_198@_free_result( $cur );
$this->bindArrayToObject( $array,$object);
return true;
}
else {
return false;
}
}
else {
if ($cur = $this->justquery($sql)) {
if ($object = @R_404_198@_fetch_object( $cur )) {
@R_404_198@_free_result( $cur );
return true;
}
else {
$object = null;
return false;
}
}
else {
return false;
}
}
}

function bindArrayToObject( $array,&$obj) {
if (!is_array( $array ) || !is_object( $obj )) {
return (false);
}

foreach (get_object_vars($obj) as $k => $v) {
if( substr( $k,1 ) != '_' ) {
$ak = $k;
if (isset($array[$ak])) {
$obj->$k = $array[$ak];
}
}
}

return true;
}

function formatCSVCell($data) {
$useQuotes = false;

$quotable = array (
"\"" => "\"\"","," => ","\n" => "\n"
);

foreach ($quotable as $char => $repl) {
if (eregi($char,$data)) {
$useQuotes = true;
} else {}
}

if ($useQuotes == true) {
foreach ($quotable as $char => $repl) {
$data = str_replace($char,$repl,$data);
}

$data = "\"" . $data . "\"";
}
else {

}

return $data;
}
}

原文链接:https://www.f2er.com/php/528815.html

猜你在找的PHP相关文章