class mysqltool{
private $link=NULL;//私有化属性防止被外界改变
function __construct($peizhi){
$host=$peizhi['host'];
$port=$peizhi['port'];
$user=$peizhi['user'];
$password=$peizhi['password'];
$charset=$peizhi['charset'];
$dbname=$peizhi['dbname'];
$link=mysql_connect($peizhi);//开始连接数据库,返回连接资源
mysql_query("set names {$this->charset}",$link);//设置数据库的字符编码格式
mysql_query("use {$this->dbname}",$link);//选择要是使用的数据库
}
//查询多行数据,以二维数组返回
function getrows($sql){
$result=mysql_query($sql,$this->link);
if($result===false){
echo "<p>发生错误,详情参考:";
echo "<br>错误语句{$sql}";
echo "<br>错误信息".mysql_error();
echo "<br>错误代码".mysql_errno();
die();
}
else {
$arr_data=array();
while($res=mysql_fetch_assoc($result)){
$arr_data[]=$res;
return $arr_data;
}
}
}
$conf1=array(
"host" => "localhost",
"port" => "3306",
"user" => "root",
"password" => "1034",
"charset" => "utf8",
"dbname" => "bookguanli");
$shujuku=new mysqltool($conf1);
$sql1="select * from bookcategory";
$shuzu=$shujuku->getrows($sql1);
echo"<table board=1>";
foreach ($shuzu as $val){
echo "<tr>";
echo "<td>{$val['category_id']}</td>";
echo "<td>{$val['category']}</td>";
echo "<td>{$val['parent_id']}</td>";
echo "<tr>";
}
echo"</table>";
private $link=NULL;//私有化属性防止被外界改变
function __construct($peizhi){
$host=$peizhi['host'];
$port=$peizhi['port'];
$user=$peizhi['user'];
$password=$peizhi['password'];
$charset=$peizhi['charset'];
$dbname=$peizhi['dbname'];
$link=mysql_connect($peizhi);//开始连接数据库,返回连接资源
mysql_query("set names {$this->charset}",$link);//设置数据库的字符编码格式
mysql_query("use {$this->dbname}",$link);//选择要是使用的数据库
}
//查询多行数据,以二维数组返回
function getrows($sql){
$result=mysql_query($sql,$this->link);
if($result===false){
echo "<p>发生错误,详情参考:";
echo "<br>错误语句{$sql}";
echo "<br>错误信息".mysql_error();
echo "<br>错误代码".mysql_errno();
die();
}
else {
$arr_data=array();
while($res=mysql_fetch_assoc($result)){
$arr_data[]=$res;
return $arr_data;
}
}
}
$conf1=array(
"host" => "localhost",
"port" => "3306",
"user" => "root",
"password" => "1034",
"charset" => "utf8",
"dbname" => "bookguanli");
$shujuku=new mysqltool($conf1);
$sql1="select * from bookcategory";
$shuzu=$shujuku->getrows($sql1);
echo"<table board=1>";
foreach ($shuzu as $val){
echo "<tr>";
echo "<td>{$val['category_id']}</td>";
echo "<td>{$val['category']}</td>";
echo "<td>{$val['parent_id']}</td>";
echo "<tr>";
}
echo"</table>";