phpmysql.php
<?php
#$conn=mysql_connect("localhost","root","123456");
#mysql_select_db("pps");
#$sql="SELECT *FROM pic";
#$query=mysql_query($sql);
#$row=mysql_fetch_assoc($query);
#echo "<pre>";
#print_r($row);
#echo "</pre>";
//mysql_close($conn);
class mysql{
private $host="127.0.0.1";
private $user="root";
private $pwd="123456";
private $db="pps";
private $conn;
static $connect;
function __construct(){
//if(!self::$connect){
echo "connect";
$this->connect();
self::$connect=TRUE;
//}
}
function connect(){
$host=$this->host;
$user=$this->user;
$pwd=$this->pwd;
$this->conn=mysql_connect($host,$user,$pwd);
$db=mysql_select_db($this->db,$this->conn);
}
function query($sql,$type=""){
$query=mysql_query($sql);
if($type){
if($type=="assoc"){
$row=mysql_fetch_assoc($query);
}elseif($type=="all"){
$i=0;
while($array=mysql_fetch_assoc($query)){
$i=$i+1;
$row[$i]=$array;
}
}elseif($type=="1"){
$row=mysql_fetch_row($query);
$row=$row[0];
}
}else{
mysql_query($sql);
}
return $row;
}
function __destruct(){
mysql_close($this->conn);
}
}
/*$mysql=new mysql;
$array=$mysql->query("select addtime from pic","assoc");
echo "<pre>";
print_r($array);
echo "</pre>";
$array=$mysql->query("select addtime from pic","assoc");
echo "<pre>";
print_r($array);
echo "</pre>";*/
?>
class.php
<?php
include "phpmysql.php";
class php extends mysql{
function __construct(){
parent::__construct();
}
function get_test(){
$row=$this->query("select *from pic","all");
return $row;
}
}
$php=new php;
$row=$php->get_test();
echo "<pre>";
print_r($row);
echo "</pre>";