随笔 - 61  文章 - 48  trackbacks - 0
<2007年9月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

留言簿(10)

随笔档案

收藏网站

搜索

  •  

最新评论

阅读排行榜

评论排行榜

test数据库中test表单:

create table test(
id int(5) not null primary key auto_increment,
name varchar(8) not null,
sex varchar(2) not null,
age int(2) not null,
);

表定义的不太规范,不要介意!

mysql类的定义:(mysql.inc)
<?php
class mysql
{  
       var $link;
       var $insert_re;
       var $get_val;
       function conn_mysql($host,$user,$pass)
       {
              $a=mysql_pconnect($host,$user,$pass) or die("unable conn MySQL");
              $this->link=$a;
       }
 
       function select_db($dbname)
       {
              mysql_select_db($dbname) or die("unbale select database!");
       }
 
       function insert($tbname,$cell,$val)
       {
              if($cell=="" or !isset($cell) or $cell==1)
              {
                     $sql="insert into ".$tbname." values(".$val.")";
                     $re=mysql_query($sql);
                     $this->insert_re=$re;
              }
              else
              {
                     $sql="insert into ".$tbname."(".$cell.") values(".$val.")";
                     $re=mysql_query($sql);
                     $this->insert_re=$re;
              }
       }
 
       function get_values($tbname,$cell,$where)
       {
              unset($this->get_val);     //格式化get_val;
 
              if($cell==1)
              {
                     if($where==1) //没有控制条件时
                     {
                            $sql="select * from ".$tbname;
                            $re=mysql_query($sql);
                            while($row=mysql_fetch_row($re))
                            {
                                   foreach($row as $key=>$val)
                                   {
                                          $this->get_val[]=$val;
                                   }
                            }
                     }
                    
                     else
                     {
                            $sql="select * from ".$tbname." where ".$where; //有控制条件时
                            $re=mysql_query($sql);
                            while($row=mysql_fetch_row($re))
                            {
                                   foreach($row as $key=>$val)
                                   {
                                          $this->get_val[]=$val;
                                   }
                            }
                     }
              }
 
              else
              {
                     if($where==1) //没有控制条件时  310-150 350-020
                     {
                            $sql="select ".$cell." from ".$tbname;
                            $re=mysql_query($sql);
                            while($row=mysql_fetch_row($re))
                            {
                                   foreach($row as $key=>$val)
                                   {
                                          $this->get_val[]=$val;
                                   }
                            }
                     }
                    
                     else
                     {
                            $sql="select ".$cell." from ".$tbname." where ".$where; //有控制条件时
                            $re=mysql_query($sql);
                            while($row=mysql_fetch_row($re))
                            {
                                   foreach($row as $key=>$val)
                                   {
                                          $this->get_val[]=$val;
                                   }
                            }
                     }
              }
       }
 
}
?>
 
 
综合实例:
<?php
include("mysql.inc");
$sql=new mysql;
$sql->conn_mysql("localhost","root","123");
$sql->select_db("test");
$sql->insert("test","name,sex,age","'dx_andy','男',23");
$sql->get_values("test","name","name='小'");
if(isset($sql->get_val))
{
       print_r($sql->get_val);
}
else echo "对不起,数据库中没有此数据";
echo "<br>";
$sql->get_values("test",1,1);
if($sql->get_val)
{
       print_r($sql->get_val);
}
else echo "对不起,数据库中没有此数据";
echo "<br>";
?>

 

posted on 2007-09-26 09:17 php一道 阅读(1552) 评论(0)  编辑 收藏 引用 网摘

只有注册用户登录后才能发表评论。
网站导航: