网络编程 
首页 > 网络编程 > 浏览文章

php入门之连接mysql数据库的一个类

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

项目结构:

php入门之连接mysql数据库的一个类

运行效果;

php入门之连接mysql数据库的一个类
conn.php
复制代码 代码如下:
<?php
class ConnectionMySQL{
//主机
private $host="localhost";
//数据库的username
private $name="root";
//数据库的password
private $pass="";
//数据库名称
private $table="phptest";
//编码形式
private $ut="utf-8";
//构造函数
function __construct(){
$this->ut=$ut;
$this->connect();
}
//数据库的链接
function connect(){
$link=mysql_connect($this->host,$this->name,$this->pass) or die ($this->error());
mysql_select_db($this->table,$link) or die("没该数据库:".$this->table);
mysql_query("SET NAMES '$this->ut'");
}
function query($sql, $type = '') {
if(!($query = mysql_query($sql))) $this->show('Say:', $sql);
return $query;
}
function show($message = '', $sql = '') {
if(!$sql) echo $message;
else echo $message.'<br>'.$sql;
}
function affected_rows() {
return mysql_affected_rows();
}
function result($query, $row) {
return mysql_result($query, $row);
}
function num_rows($query) {
return @mysql_num_rows($query);
}
function num_fields($query) {
return mysql_num_fields($query);
}
function free_result($query) {
return mysql_free_result($query);
}
function insert_id() {
return mysql_insert_id();
}
function fetch_row($query) {
return mysql_fetch_row($query);
}
function version() {
return mysql_get_server_info();
}
function close() {
return mysql_close();
}
//向$table表中插入值
function fn_insert($table,$name,$value){
$this->query("insert into $table ($name) value ($value)");
}
//根据$id值删除表$table中的一条记录
function fn_delete($table,$id,$value){
$this->query("delete from $table where $id=$value");
echo "id为". $id." 的记录被成功删除!";
}
}
$db = new ConnectionMySQL();
$db->fn_insert('test','id,name,sex',"'','hongtenzone','M'");
$db->fn_delete('test', 'id', 1);
?>

上一篇:php 多关键字 高亮显示实现代码
下一篇:洪恩在线成语词典小偷程序php版
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网