<?php
class dbname{
private $localhost;
private $root;
private $pass;
private $db_name;
public function __construct($localhost,$root,$pass,$db_name){
$this->localhost=$localhost;
$this->root=$root;
$this->pass=$pass;
$this->db_name=$db_name;
$this->query();
}
private function query(){
mysql_connect($this->localhost,$this->root,$this->pass);
mysql_select_db($this->db_name);
mysql_query("set names utf8");
}
public function getAll($sql){
$result = mysql_query($sql);
$rows=array();
while($row = mysql_fetch_assoc($result)){//返回的是多条记录
$rows[]=$row;
}
return $rows;
}
class dbname{
private $localhost;
private $root;
private $pass;
private $db_name;
public function __construct($localhost,$root,$pass,$db_name){
$this->localhost=$localhost;
$this->root=$root;
$this->pass=$pass;
$this->db_name=$db_name;
$this->query();
}
private function query(){
mysql_connect($this->localhost,$this->root,$this->pass);
mysql_select_db($this->db_name);
mysql_query("set names utf8");
}
public function getAll($sql){
$result = mysql_query($sql);
$rows=array();
while($row = mysql_fetch_assoc($result)){//返回的是多条记录
$rows[]=$row;
}
return $rows;
}
本文详细介绍了如何使用PHP连接数据库并执行SQL查询的过程,包括配置数据库连接参数、执行SQL语句获取数据,并展示了如何使用mysql_query函数进行操作。

被折叠的 条评论
为什么被折叠?



