managementclass php calls mysql data dbclass class
Jul 29, 2016 am 08:45 AM復制代碼 代碼如下:
class dbClass{ //開始數(shù)據(jù)庫類
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(){ //這個函數(shù)用于連接數(shù)據(jù)庫
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_c character_set_results=$dbcharset, character_set_client=binary");
}
}
if($this->version() > '5.0.1') {
mysql_query("SET sql_mode=''");
}
return $this->link;
}
function select(){ //這個函數(shù)用于選擇數(shù)據(jù)庫
mysql_select_db($this->database,$this->link);
}
function query($sql){ //這個函數(shù)用于送出查詢語句并返回結(jié)果,常用。
if($this->result=mysql_query($sql,$this->link)) return $this->result;
else {
$this->halt("SQL語句錯誤: $sql
錯誤信息: ".mysql_error());
return false;
}
}
/*
以下函數(shù)用于從結(jié)果取回數(shù)組,一般與 while()循環(huán)、$db->query($sql) 配合使用,例如:
$result=query("select * from mytable");
while($row=$db->getarray($result)){
echo "$row[id] ";
}
*/
function getarray($result){
return @mysql_fetch_array($result);
}
/*
以下函數(shù)用于取得SQL查詢的第一行,一般用于查詢符合條件的行是否存在,例如:
用戶從表單提交的用戶名$username、密碼$password是否在用戶表“user”中,并返回其相應的數(shù)組:
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));
}
/*
以下函數(shù)返回符合查詢條件的總行數(shù),例如用于分頁的計算等要用到,例如:
$totlerows=$db->getcount("select * from mytable");
echo "共有 $totlerows 條信息。";
*/
function getcount($sql){
return @mysql_num_rows($this->query($sql));
}
/*
以下函數(shù)用于更新數(shù)據(jù)庫,例如用戶更改密碼:
$db->update("update user set password='$new_password' where userid='$userid' ");
*/
function update($sql){
return $this->query($sql);
}
/*
以下函數(shù)用于向數(shù)據(jù)庫插入一行,例如添加一個用戶:
$db->insert("insert into user (userid,username,password) values (null,'$username','$password')");
*/
function insert($sql){
return $this->query($sql);
}
function getid(){ //這個函數(shù)用于取得剛插入行的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();
以上就介紹了managementclass php調(diào)用mysql數(shù)據(jù) dbclass類,包括了managementclass方面的內(nèi)容,希望對PHP教程有興趣的朋友有所幫助。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Common problems and solutions for PHP variable scope include: 1. The global variable cannot be accessed within the function, and it needs to be passed in using the global keyword or parameter; 2. The static variable is declared with static, and it is only initialized once and the value is maintained between multiple calls; 3. Hyperglobal variables such as $_GET and $_POST can be used directly in any scope, but you need to pay attention to safe filtering; 4. Anonymous functions need to introduce parent scope variables through the use keyword, and when modifying external variables, you need to pass a reference. Mastering these rules can help avoid errors and improve code stability.

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre
