php调用mysql数据 dbclass类_PHP
- WBOYOriginal
- 2016-06-01 12:16:431010browse
复制代码 代码如下:
class dbClass{ //开始数据库类
var $username;
var $password;
var $database;
var $hostname;
var $link;
var $result;
function dbClass($username,$password,$database,$hostname="localhost"){
$this->username=$username;
$this->password=$password;
$this->database=$database;
$this->hostname=$hostname;
}
function connect(){ //这个函数用于连接数据库
if(!$this->link=mysql_connect($this->hostname,$this->username,$this->password))
$this->halt("Sorry,can not connect to database");
if($this->version() > '4.1') {
global $dbcharset,$charset;
if(!$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
$dbcharset = str_replace('-', '', $charset);
}
if($dbcharset) {
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary");
}
}
if($this->version() > '5.0.1') {
mysql_query("SET sql_mode=''");
}
return $this->link;
}
function select(){ //这个函数用于选择数据库
mysql_select_db($this->database,$this->link);
}
function query($sql){ //这个函数用于送出查询语句并返回结果,常用。
if($this->result=mysql_query($sql,$this->link)) return $this->result;
else {
$this->halt("SQL语句错误: $sql
错误信息: ".mysql_error());
return false;
}
}
/*
以下函数用于从结果取回数组,一般与 while()循环、$db->query($sql) 配合使用,例如:
$result=query("select * from mytable");
while($row=$db->getarray($result)){
echo "$row[id] ";
}
*/
function getarray($result){
return @mysql_fetch_array($result);
}
/*
以下函数用于取得SQL查询的第一行,一般用于查询符合条件的行是否存在,例如:
用户从表单提交的用户名$username、密码$password是否在用户表“user”中,并返回其相应的数组:
if($user=$db->getfirst("select * from user where username='$username' and password='$password' "))
echo "欢迎 $username ,您的ID是 $user[id] 。";
else
echo "用户名或密码错误!";
*/
function getfirst($sql){
return @mysql_fetch_array($this->query($sql));
}
/*
以下函数返回符合查询条件的总行数,例如用于分页的计算等要用到,例如:
$totlerows=$db->getcount("select * from mytable");
echo "共有 $totlerows 条信息。";
*/
function getcount($sql){
return @mysql_num_rows($this->query($sql));
}
/*
以下函数用于更新数据库,例如用户更改密码:
$db->update("update user set password='$new_password' where userid='$userid' ");
*/
function update($sql){
return $this->query($sql);
}
/*
以下函数用于向数据库插入一行,例如添加一个用户:
$db->insert("insert into user (userid,username,password) values (null,'$username','$password')");
*/
function insert($sql){
return $this->query($sql);
}
function getid(){ //这个函数用于取得刚插入行的id
return mysql_insert_id();
}
function num_rows($query) {
$query = mysql_num_rows($query);
return $query;
}
function num_fields($query) {
return mysql_num_fields($query);
}
function free_result($query) {
return mysql_free_result($query);
}
function version() {
return mysql_get_server_info();
}
function close() {
return mysql_close();
}
function halt($message = '') {
return $message;
}
}
$db=new dbClass("$db_username","$db_password","$db_database","$db_hostname");
$db->connect();
$db->select();
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn