The front-end has been using PHP5, and indeed use it particularly cool, now in order to be able to run on my virtual host, have to change to PHP4. These library classes I used to send in Phpchian, the address is http://www.phpchina.com/bbs/viewthread.php?tid=5687&highlight=. (A few days ago on the internet search, found a lot of these articles reproduced I did not indicate the source, and my copyright has been deleted, gas halo. )
The database operation class was rewritten yesterday, just as I could use it to simplify the Zend framework.
The code is as follows:
/**
* filename:DB_Mysql.class.php
* @package:p Hpbean
* @author:feifengxlq<[email]feifengxlq@gmail.com[/email]>
* @copyright: Copyright 2006 FEIFENGXLQ
* @license: Version 1.2
* create:2006-5-30
* Modify:2006-10-19 by FEIFENGXLQ
* Description:the interface of MySQL.
*
* Example:
*////////////select Action (first mode)//////////////////////////////
$mysql =new db_mysql ("localhost", "root", "root", "root");
$rs = $mysql->query ("SELECT * from Test");
for ($i =0; $i < $mysql->num_rows ($RS); $i + +)
$record [$i]= $mysql->seek ($i);
Print_r ($record);
$mysql->close ();
*////////////select Action (Second mode)//////////////////////////////
$mysql =new db_mysql ("localhost", "root", "root", "root");
$rs = $mysql->execute ("SELECT * from Test");
Print_r ($RS);
$mysql->close ();
*/////////////insert action////////////////////////////
$mysql =new db_mysql ("localhost", "root", "root", "root");
$mysql->query ("INSERT into Test (username) values (' Test from my Db_mysql ')");
printf ("%s", $mysql->insert_id ());
$mysql->close ();
*/
Class mysql{
/* private:connection Parameters */
var $host = "localhost";
var $database = "";
var $user = "root";
var $password = "";
/* private:configuration Parameters */
var $pconnect =false;
var $debug =false;
/* Private:result Array and current row number */
var $link _id=0;
var $query _id=0;
var $record =array ();
/**
* Construct
*
* @param string $host
* @param string $user
* @param string $password
* @param string $database
*/
function __construct ($host = "localhost", $user = "root", $password = "", $database = "")
{
$this->set ("host", $host);
$this->set ("user", $user);
$this->set ("password", $password);
$this->set ("database", $database);
$this->connect ();
}
/**
* Set the value for the param of this class
*
* @param string $var
* @param string $value
*/
function set ($var, $value)
{
$this $var = $value;
}
/**
* Connect to a MySQL Server,and choose the database.
*
* @param string $database
* @param string $host
* @param string $user
* @param string $password
* @return link_id
*/
Function Connect ($database = "", $host = "", $user = "", $password = "")
{
if (!empty ($database)) $this->set ("database", $database);
if (!empty ($host)) $this->set ("host", $host);
if (!empty ($user)) $this->set ("user", $user);
if (!empty ($password)) $this->set ("password", $password);
if ($this->link_id==0)
{
if ($this->pconnect)
$this->link_id= @mysql_pconnect ($this->host, $this->user, $this->password);
Else
$this->link_id= @mysql_connect ($this->host, $this->user, $this->password);
if (! $this->link_id)
Die ("Mysql Connect Error in". __function__. " (): ". Mysql_errno ().": ". Mysql_error ());
if (! @mysql_select_db ($this->database, $this->link_id))
Die ("Mysql Select Database Error in". __function__. " (): ". Mysql_errno ().": ". Mysql_error ());
}
return $this->link_id;
}
/**
* Query a SQL into the database
*
* @param string $strsql
* @return query_id
*/
function query ($strsql = "")
{
if (empty ($strsql)) Die ("Mysql Error:". __function__. " () strSQL is empty! ");
if ($this->link_id==0) $this->connect ();
if ($this->debug) printf ("Debug Query sql:%s", $strsql);
$this->query_id= @mysql_query ($strsql, $this->link_id);
if (! $this->query_id) die ("Mysql Query Fail,invalid sql:". $strsql. ".");
return $this->query_id;
}
/**
* Query a SQL into the database,while it was differernt from the query () method,
* This method would return a record (array);
*
* @param string $strsql
* @param string $style
* @return $record is a array ()
*/
function Execute ($strsql, $style = "Array")
{
$this->query ($strsql);
if (!empty ($this->record)) $this->record=array ();
$i = 0;
if ($style = = "Array") {
while ($temp = @mysql_fetch_array ($this->query_id)) {
$this->record[$i]= $temp;
$i + +;
}
}else{
while ($temp = @mysql_fetch_object ($this->query_id)) {
$this->record[$i]= $temp;
$i + +;
}
}
Unset ($i);
Unset ($temp);
return $this->record;
}
/**
* Seek,but not equal to Mysql_data_seek. This methord would return a list.
*
* @param int $pos
* @param string $style
* @return Record
*/
function Seek ($pos =0, $style = "Array")
{
if (! @mysql_data_seek ($this->query_id, $pos))
Die ("Error in". __function__. " (): Can not seek to row ". $pos."! ");
$result =@ ($style = = "Array")? Mysql_fetch_array ($this->query_id): mysql_fetch_object ($this->query_id);
if (! $result) Die ("Error in". __function__. (): Can not fetch data! ");
return $result;
}
/**
* Free the result of query
*
*/
function free ()
{
if ($this->query_id) & ($this->query_id!=0)) @mysql_free_result ($this->query_id);
}
/**
* Evaluate the result (size, width)
*
* @return Num
*/
function Affected_rows ()
{
Return @mysql_affected_rows ($this->link_id);
}
function Num_rows ()
{
Return @mysql_num_rows ($this->query_id);
}
function Num_fields ()
{
Return @mysql_num_fields ($this->query_id);
}
function insert_id ()
{
Return @mysql_insert_id ($this->link_id);
}
function Close ()
{
$this->free ();
if ($this->link_id!=0) @mysql_close ($this->link_id);
if (Mysql_errno ()!=0) die ("MySQL Error:". Mysql_errno (). ":". Mysql_error ());
}
function Select ($strsql, $number, $offset)
{
if (empty ($number)) {
return $this->execute ($strsql);
}else{
return $this->execute ($strsql. ' Limit ' $offset. ', '. $number);
}
}
function __destruct ()
{
$this->close ();
$this->set ("User", "");
$this->set ("host", "");
$this->set ("Password", "" ");
$this->set ("Database", "");
}
}
?>
On this basis, I encapsulate Sidu (Select,insert,update,delete) four basic operations as a simplified module of the Zend Framework. The code is as follows (this does not write the comment, lazy writing.) ):
Class module{
var $mysql;
var $tbname;
var $debug =false;
function __construct ($tbname) {
if (!is_string ($tbname)) Die (' Module need a args of TableName ');
$this->tbname= $tbname;
$this->mysql=phpbean::registry (' db ');
}
function _setdebug ($debug =true) {
$this->debug= $debug;
}
function Add ($row) {
if (!is_array ($row)) Die (' module Error:row should is an array ');
$strsql = ' insert INTO '. $this->tbname. ' `';
$keys = ";
$values = ";
foreach ($row as $key = = $value) {
$keys. = '. $key. ' `,';
$values. = ' \ '. $value. ' \'';
}
$keys =rtrim ($keys, ', ');
$values =rtrim ($values, ', ');
$strsql. = ' (' $keys. ') VALUES ('. $values. ') ';
if ($this->debug) echo '. $strsql. ';
$this->mysql->query ($strsql);
return $this->mysql->insert_id ();
}
function query ($strsql) {
return $this->mysql->execute ($strsql);
}
function count ($where = ") {
$strsql = ' SELECT count (*) as num from '. $this->tbname. ' ` ';
if (!empty ($where)) $strsql. = $where;
$rs = $this->mysql->execute ($strsql);
return $rs [0][' num '];
}
function Select ($where = ") {
$strsql = ' select * from '. $this->tbname. ' ` ';
if (!empty ($where)) $strsql. = $where;
return $this->mysql->execute ($strsql);
}
function Delete ($where = ") {
if (empty ($where)) die (' error:the Delete method need a condition! ');
return $this->mysql->query (' delete from '. $this->tbname. ' ". $where);
}
function Update ($set, $where) {
if (empty ($where)) Die (' error:the Update method need a condition! ');
if (!is_array ($set)) Die (' Error:set must is an array! ');
$strsql = ' Update '. $this->tbname. ' ` ';
Get a string of set
$strsql. = ' Set ';
foreach ($set as $key = = $value) {
$strsql. = '. $key. ' ' =\ '. $value. ' \',';
}
$strsql =rtrim ($strsql, ', ');
return $this->mysql->query ($strsql. ' '. $where);
}
function Detail ($where) {
if (empty ($where)) Die (' Error:where should not empty! ');
$rs = $this->mysql->query (' select * from '. $this->tbname. ' ". $where);
return $this->mysql->seek (0);
}
}
?>
http://www.bkjia.com/PHPjc/318723.html www.bkjia.com true http://www.bkjia.com/PHPjc/318723.html techarticle The front-end has been using PHP5, and indeed use it particularly cool, now in order to be able to run on my virtual host, have to change to PHP4. These library classes I used to send in Phpchian, the address is htt ...