mysql教程 数据库教程连接类
/*
提供一款简单实用的mysql php教程数据库连接代码哦,如果你正是php 入门者可以进来看看这款数据库代码是不是你要找的吧。
*/
class mysql{
private $host;
private $user;
private $pass;
private $dbname;
function __construct($host,$user,$pass,$dbname){
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
$this->dbname=$dbname;
$this->connect();
}
function connect(){
$link=mysql_connect($this->host,$this->user,$this->pass)or die(mysql_error());
if(mysql_select_db($this->dbname,$link)){
echo "连接成功";
}else{
echo "失败";
}
mysql_query("set names utf8");
}
function query($sql){
$run=mysql_query($sql);
return $run;
}
function fn_insert($biaoming,$zdname,$value){
$this->query("insert into $biaoming ($zdname) values ($value)");
}
}
//此款数据库连接类调用方法
$db=new mysql('localhost','root','','test');
$sql ="select * from bb ";
$query = $db->query( $sql );
while ( $rs = mysql_fetch_array( $query ) )
{
echo '....';
}
//再利用这个类来保存数据
$db->fn_insert('test','names',''111cn.net'');