标签:res more 构造 login new pass roo http span
思路:构造函数完成数据库连接,增删改一个方法,查询一条记录一个方法,查询多条一个方法,sql执行失败则返回提示,并交出sql语句方便查错
代码:
<?
class dbcontroll{
private $links;
private $sql;
public function __construct($host,$user,$password,$db){
$con = mysql_connect($host,$user,$password);
if(!$con){
echo ‘主机连接信息错误!‘;die;
}
$this->links = mysql_select_db($db,$con);
if(!$this->links){
echo ‘数据库连接错误!‘;die;
}
}
//增删改
public function query($sql){
$res = mysql_query($sql);
if(!$res){
echo ‘sql执行错误‘.$sql;
}else{
echo ‘执行成功!‘;
}
}
//查询多条数据
public function getmore($sql){
$res = mysql_query($sql);
if(!$res){
echo ‘sql执行错误‘.$sql;
}else{
$arr = array();
while($row = mysql_fetch_row($res)){
array_push($arr,$row);
}
return $arr;
}
}
//查询一条数据
public function find($sql){
$res = mysql_query($sql);
if(!$res){
echo ‘sql执行错误‘.$sql;
}else{
while($row = mysql_fetch_row($res)){
return $row;
}
}
}
}
$sql = "select * from login where id = 3";
$db = new dbcontroll(‘localhost‘,‘root‘,‘root‘,‘login‘);
$a = $db->find($sql);
var_dump($a);
?>
效果图:
标签:res more 构造 login new pass roo http span
原文地址:http://www.cnblogs.com/chengzhongde/p/6893683.html