关于MYSQL的select使用面向对象的问题
<p ><strong>milkzhou</strong> 发表于 2009-9-15 23:56</p>
<h3>关于MYSQL的select使用面向对象的问题</h3>利用面向对象来使用select语句。
同时希望通过mysql_fetch_row来进行值的返回,但却什么也没有返回,
红色的代码是主要的部分, 为了完整,我将所有的,都贴的出来。
另,附
在MYSQL在PHP中的使用里,我们可以利用mysql_fetch_row来返回数组的值,
具体如下:
$sql="SELECT * FROM `test`";
$query=mysql_query($sql,$conn);
$row=mysql_fetch_row($query);
print_r($row);
那如果将这个功能用面向对象,怎样才能做得到呢?
我研究了一个下午,都不知道怎么写才对,希望大家一起研究研究,谢谢。
<?php
/*
* mysql_class
*/
class mysql{
//由于mysql安全性要高,所以就需要用到private
private $host;
private $name;
private $pass;
function __construct($host,$name,$pass,$table,$ut){//初始化
$this->host=$host;
$this->name=$name;
$this->pass=$pass;
$this->table=$table;//数据库的表
$this->ut=$ut;
$this->connect();
}
function connect(){
$link=mysql_connect($this->host,$this->name,$this->pass) or die ($this->error());
mysql_select_db($this->table,$link) or die("没该数据库:".$this->table);
mysql_query("SET NAMES '$this->ut'");//ut8的模式
}
function query($sql, $type = '') {
if(!($query = mysql_query($sql))) $this->show('Say:', $sql);
return $query;
}
function show($message = '', $sql = '') {
if(!$sql) echo $message;
else echo $message.'<br>'.$sql;
}
function affected_rows() {
return mysql_affected_rows();
}
function result($query, $row) {
return mysql_result($query, $row);
}
function num_rows($query) {
return @mysql_num_rows($query);
}
function num_fields($query) {
return mysql_num_fields($query);
}
function free_result($query) {
return mysql_free_result($query);
}
function insert_id() {
return mysql_insert_id();
}
function fetch_row($query) {
return mysql_fetch_row($query);
}
function version() {
return mysql_get_server_info();
}
function close() {
return mysql_close();
}
//=============增加其它的功能,功能性的方法,以fn开头
function fn_insert($table,$name,$value){//$table:表名,$name:名字,$value:值
$this->query("insert into $table ($name) value ($value)");
}
function fn_select($table){
$this->fetch_row($this->query("select * from $table"));
}
}
$db=new mysql('localhost', 'root', 'root','new1','GBK');
$db->fn_insert('new2','id,uid,salary',"'','我插入d的信息','7000.9'");
print_r($db->fn_select('new2'));
?>
页:
[1]