class Mysql{

private $host;

private $user;

private $pwd;

private $dbName;

private $charset;

private $conn=null;//保存连接资源

public function __construct(){

//应该是在构造方法里,读取配置文件

//然后根据配置文件来设置私有属性

//此处还没有配置文件,就直接赋值

$this->host='localhost';

$this->user='root';

$this->pwd='';

$this->dbName='test';

//执行连接

$this->connect($this->host,$this->user,$this->pwd);

//切换库

$this->switchDB($this->dbName);

//设置字符节

$this->setChar($this->charset);

}

//负责连接

private function connect($h,$u,$p){

$conn=mysql_connect($h,$u,$p);

$this->conn=$conn;

}

//负责切换数据库,网站大了会涉及多个库

public function switchDB($db){

$sql='use' . $db;

$this->query($sql);

}

//负责设置字符集

public function setChar($char){

$sql='set names' . $char;

$this->query($sql);

}

//负责发送sql语句结果

public function query($sql){

return mysql_query($sql,$this->conn);

}

//负责获取多行多列的select

public function getAll($sql){

}

}

//测试是否连接成功 Resource id #3

$mysql=new Mysql();

print_r($mysql);

$sql="insert into user values ( 5, '小明', 23 )";

if($mysql->query($sql)){

echo'插入成功!';

}else{

echo'插入失败!';

}

//代码结束 运行结果为 插入失败!

?>

展开

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐