Support PHP4, PHP5 MySQL database operation class _php skills
Source: Internet
Author: User
The front end has been using PHP5, it is really very cool to use, now in order to be able to run on my virtual host, had 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 that a lot of these articles reproduced me did not explain the source, and my copyright has been deleted, gas dizzy.) )
The database operations class was rewritten yesterday, just as I was able to simplify the Zend framework.
The code is as follows:
<?php
/**
* 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 (//////////////////////////////)
$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 ();
/**
* 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 are 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 ());
}
On this basis, I conveniently encapsulate Sidu (Select,insert,update,delete) four basic operations as a simplified module for the Zend Framework. The code is as follows (this does not write a comment, lazy write.) ):
?
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 ' $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 be a 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);
}
}
?>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service