php 面向对象mysql类,面向对象的mysql数据库操作php类

搜索热词

下面是编程之家 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 = MysqL_connect($this->host,$this->username,$this->password) or die("Could not connect to Database");

MysqL_select_db($this->databaseName);

}

function justquery($sql) {

$this->queries[] = $sql;

return MysqL_query($sql,$this->link);

}

function loadResult($sql) {

if (!($cur = $this->justquery($sql))) {

return null;

}

$ret = null;

if ($row = MysqL_fetch_row( $cur )) {

$ret = $row[0];

}

MysqL_free_result( $cur );

return $ret;

}

function loadFirstRow($sql) {

if (!($cur = $this->justquery($sql))) {

return null;

}

$ret = null;

if ($row = MysqL_fetch_object( $cur )) {

$ret = $row;

}

MysqL_free_result( $cur );

return $ret;

}

function insertid() {

return MysqL_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 = MysqL_query($query,$this->link);

if (!$answer) {

$this->errors[] = MysqL_error($this->link);

}

else {

if ($returns != false) {

if (MysqL_num_rows($answer) > 0){

while ($row = MysqL_fetch_object($answer)) {

if ($key != ""){

$result[$index][$row->$key] = $row;

}

else {

$result[$index][] = $row;

}

}

} else {}

} else {}

}

}

break;

case false:

$this->queries[] = $sql;

$answer = MysqL_query($sql,$this->link);

if (!$answer) {

$this->errors[] = MysqL_error($this->link);

$result = false;

}

else {

if ($returns != false) {

if (MysqL_num_rows($answer) > 0){

while ($row = MysqL_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 = MysqL_fetch_assoc( $cur )) {

MysqL_free_result( $cur );

$this->bindArrayToObject( $array,$object);

return true;

}

else {

return false;

}

}

else {

if ($cur = $this->justquery($sql)) {

if ($object = MysqL_fetch_object( $cur )) {

MysqL_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;

}

}

?>

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值