php数据库操作示例
PHP #数据库操作2012-10-29 11:13
直接上代码了。
<?php
/*Created By RexLee MySQL类
http://yige.org/php/
*/
class MySQL {
private $host;
private $name;
private $password;
private $dbname; //数据库名
private $db; //数据库连接句柄
private $link; //连接名
public $errmsg; //错误信息
public function __construct($server,$dbuser,$psw){
$this->open($server,$dbuser,$psw);
}
private function open($server,$dbuser,$psw){//连接主机
$this->host=$server;
$this->name=$dbuser;
$this->password=$psw;
$this->link=mysql_connect($this->host,$this->name,$this->password) or die("主机连接失败!");;
}
public function to2DArray($result){
$_2DArray=Array();
$arr=new ArrayObject($_2DArray);
while($row=mysql_fetch_array($result)){
$arr->append($row);
}
return $arr ;
}
public function opendb($database,$charset){//连接数据库
$this->dbname=$database;
mysql_query("set names ".$charset);//设置字符集
$this->db=mysql_select_db($this->dbname,$this->link);
if (!$this->db) {
$this->errmsg="连接数据库错误!";
}
}
public function query($sql) {
$result=mysql_query($sql);
if (!$result) {
$this->errmsg="语句运行错误!";
}
return $result;
}
private function __call($n,$v){//错误方法吸收
return "不存在".$n."()方法";
}
}
//Example
// $c=new MySQL("localhost","root","lijun");
// $c->opendb("demo", "utf8");
// $r=$c->query("select * from test");
// echo $r,'<br>';
// $ar=$c->to2DArray($r);
// print_r($ar[0]);
// echo '<br>';
// print_r($ar[1]);
// echo '<br>';
// echo $c->hi();
?>相关文章
- php+mysql缓存技术 2012/10/29
- 在PHP+MySQL中实现Memcache缓存 2012/10/29
- PHP判断文件存在 2012/10/29
- PHP在客户端禁用cookie的情况下使用session 2012/10/29
- php图片采集类 2012/10/29
- PHP获取二进制文件头快速判断文件类型 2012/10/29
- PHP中echo(),print(),print_r()的区别 2012/10/29
- PHP中执行系统外部命令 2012/10/29
- PHP Sockets编程的两个例子 2012/10/29
- php实现在线端口扫描器 2012/10/29