Home Backend Development PHP Tutorial PHP calls mysql data dbclass class_PHP tutorial

PHP calls mysql data dbclass class_PHP tutorial

Jul 21, 2016 pm 03:29 PM
class mysql php VA code copy start data database kind transfer

复制代码 代码如下:

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();

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/323376.htmlTechArticle复制代码 代码如下: class dbClass{ //开始数据库类 var $username; var $password; var $database; var $hostname; var $link; var $result; function dbClass($username,$passw...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

What are the advantages of using MySQL over other relational databases? What are the advantages of using MySQL over other relational databases? May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

Laravel environment construction and basic configuration (Windows/Mac/Linux) Laravel environment construction and basic configuration (Windows/Mac/Linux) Apr 30, 2025 pm 02:27 PM

The steps to build a Laravel environment on different operating systems are as follows: 1.Windows: Use XAMPP to install PHP and Composer, configure environment variables, and install Laravel. 2.Mac: Use Homebrew to install PHP and Composer and install Laravel. 3.Linux: Use Ubuntu to update the system, install PHP and Composer, and install Laravel. The specific commands and paths of each system are different, but the core steps are consistent to ensure the smooth construction of the Laravel development environment.

MySQL vs. Oracle: Understanding Licensing and Cost MySQL vs. Oracle: Understanding Licensing and Cost May 03, 2025 am 12:19 AM

MySQL uses GPL and commercial licenses for small and open source projects; Oracle uses commercial licenses for enterprises that require high performance. MySQL's GPL license is free, and commercial licenses require payment; Oracle license fees are calculated based on processors or users, and the cost is relatively high.

How to set the rotation effect of HTML elements How to set the rotation effect of HTML elements Apr 30, 2025 pm 02:42 PM

How to set the rotation effect of an element in HTML? It can be achieved using CSS and JavaScript. 1. The transform property of CSS is used for static rotation, such as rotate(45deg). 2. JavaScript can dynamically control rotation, which is implemented by changing the transform attribute.

MySQL vs. phpMyAdmin: Understanding the Key Differences MySQL vs. phpMyAdmin: Understanding the Key Differences May 06, 2025 am 12:17 AM

MySQL is a database management system, and phpMyAdmin is a web tool for managing MySQL. 1.MySQL is used to store and manage data and supports SQL operations. 2.phpMyAdmin provides a graphical interface to simplify database management.

Navicat and MySQL: A Perfect Partnership Navicat and MySQL: A Perfect Partnership May 05, 2025 am 12:09 AM

Navicat and MySQL are perfect matches because they can improve database management and development efficiency. 1.Navicat simplifies MySQL operations and improves work efficiency through graphical interfaces and automatic generation of SQL statements. 2.Navicat supports multiple connection methods, which facilitates local and remote management. 3. It provides powerful data migration and synchronization capabilities, suitable for advanced usage. 4.Navicat helps with performance optimization and best practices such as regular backup and query optimization.

See all articles