异常简单的类,也没有进行 sql语句的容错、安全过滤,以及建立数据库之类的操作。希望在提交sql的时候已经做了安全消毒了:)
下面是演示的代码,比如我们插入一条数据
<?php
require_once( "dmysql.php" );
$DB = new DMYSQL( "localhost", "root", "", "address" );
if( $DB->execute( "INSERT INTO `table` VALUE
('','dorainm','dorainm@gmail.com')" )
{
echo "affected " . $DB->rows_num . " row(s)\n";
}
else
{
echo $DB->error;
}
?>
|
如果成功,显示“affected 1 row(s)”,失败则显示错误信息。
同样,删除信息:
<?php
require_once( "dmysql.php" );
$DB = new DMYSQL( "localhost", "root", "", "address" );
if( $DB->execute( "DELETE FROM `table` WHERE `id`<4"
)
{
echo "affected " . $DB->rows_num . " row(s)\n";
}
else
{
echo $DB->error;
}
?>
|
如果成功,显示“affected 3 row(s)”,失败则显示错误信息。
下面是查询数据库,比如:
<?php
require_once( "dmysql.php" );
$DB = new DMYSQL( "localhost", "root", "", "address" );
$results = $DB->query( "SELECT * FROM `table`" );
if( $results )
{
for( $i=0; $i<$DB->rows_num; $i++ )
{
for( $j=0; $j<$DB->cols_num; $j++ )
{
echo $results[$i][$j] . "\t";
}
echo "\n";
}
}
else
{
echo $DB->error;
}
?>
|
这个就是查询结果的例子了~
4 88250
dl88250@gmail.com 5 xiaoq xiaoqcool@gmail.com 6
dorainm
dorainm@gmail.com |
当然,例子中的二维数组,还可以这么表达,$results[$i]['id'],$results[$i]['user'],$results[$i]['email']
如果失败,就会显示相应的错误信息。
全部功能就这些,很简单但是也挺使用的。
下面是这个类的源代码:
<?php
/* DMYSQL
*
* dorainm MySQL Operation Class
* a simple class to operate MySQL database
*
*
* Author: dorainm
* E-mail: dorainm@gmail.com
* Date : 2008-11-13
*/
class DMYSQL
{
/*
* int : the fields number of query result by SELECT;
*/
public $cols_num;
/*
* int : the rows number of query result by SELECT or
* affected by DELETE, UPDATE,...;
*/
public $rows_num;
/*
* string : error information if the event appeared;
*/
public $error;
// handle of mysql
private $dbh;
// displays the error message and exit the program
private function error( $msg )
{
header('Content-Type: text/html; charset=utf-8');
?>
<html>
<head>
<title>DMYSQL ERROR</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<p> <h1> <?=$msg?> h1> </p>
<p><h2><?=@mysql_error()?></h2></p>
</body>
</html>
<?php
exit;
}
/*
* Connects to the Database Server and Selects a Database
*
* @param string $_host
* @param string $_user
* @param string $_passwd
* @param string $_database
* @param string $_charset
* @return ture or exit;
*/
function __construct( $_host, $_user, $_passwd, $_database,
$_charset=null )
{
// connect to database
$this->dbh = @mysql_connect( $_host, $_user, $_passwd );
if( !$this->dbh )
{
$this->error( "Can not CONNECT Database." );
}
// set charactor
if( !empty($_charset ) &&
version_compare(@mysql_get_server_info(), '4.1.0', '>=') )
{
$this->execute("SET NAMES '$_charset'");
}
// use database
if( !@mysql_select_db($_database,$this->dbh) )
{
$this->error("Can not USE Database.");
}
return true;
}
/*
* Disconnects the Database and Free all
*/
function __destructor()
{
@mysql_close( $this->dbh );
return;
}
/*
* Execute a SQL string
*
* @param string $sql
* @return boolean
*/
public function execute( $sql )
{
if( $retval = @mysql_query( $sql, $this->dbh ) )
{
$this->rows_num = @mysql_affected_rows();
$this->cols_num = 0;
$this->error = null;
}
else
{
$this->rows_num = $this->cols_num = 0;
$this->error = @mysql_error();
}
return $retval;
}
/*
* Query the Database with a SQL string
*
* @param string $sql
* @return false or the two-dimensional array of the result
*/
public function query( $sql )
{
$retval = null;
$results = @mysql_query( $sql, $this->dbh );
if( $this->error=@mysql_error() )
{
$this->rows_num = $this->cols_num = 0;
return false;
}
$count = 0;
while( $row = @mysql_fetch_array( $results ) )
{
$retval[$count] = $row;
$count ++;
}
$this->rows_num = mysql_num_rows( $results );
$this->cols_num = @mysql_num_fields( $results );
@mysql_free_result( $results );
return $retval;
}
/*
* Get one Value from the Database with a SQL string
*
* @param string $sql
* @param int $x : the offset value of fields
* @param int $y : the offset value of rows
* @return false or the value queryed.
*/
public function value( $sql, $x=0, $y=0 )
{
if( $results=$this->query($sql) )
{
return $results[$y][$x];
}
else
{
return false;
};
}
}
?>
|
阅读(1799) | 评论(0) | 转发(0) |