"PHP Programming Fastest Understand" sixth: MySQL database operation

Source: Internet
Author: User
Tags array copy mysql php database php programming query mysql database phpmyadmin
The answer is to make a class--the database class is generated. Through the two-time encapsulation of the function, it realizes the very good reuse. Include it when you need it.

Before talking about the PHP database, first introduce the MySQL essentials: You can use phpMyAdmin Learning database operations.

In phpMyAdmin to see the encoding of the whole selection of Chinese utf-8 is right.

MySQL database types are mainly: char (fixed space string, how much is the number of Chinese characters), varchar (variable space string, how much is initialized how many Chinese characters), int (number of integers is how many bits), float (floating point), timestamp (date , optionally created automatically when the output is already formatted date, text (text), BOOL (Boolean)

The sum () can be counted when the SQL statement is written, and the order by ' ID ' DESC LIMIT 10,10.

In phpMyAdmin Learn SQL statement additions and deletions to check on the line.

instance MySQL class
Copy CodeThe code is as follows:
<?php
Class opmysql{
Private $host = ' localhost '; Server address
Private $name = ' root '; Login account
Private $pwd = '; Login Password
Private $dBase = ' a0606123620 '; Database name
private $conn = '; Database Link Resources
Private $result = '; Result set
Private $msg = '; return results
Private $fields; return field
Private $fieldsNum = 0; Number of fields returned
Private $rowsNum = 0; Returns the number of results
Private $rowsRst = '; Returns an array of fields for a single record
Private $filesArray = Array (); return field Array
Private $rowsArray = Array (); Returns an array of results
Private $idusername =array ();
Private $idsubtitle =array ();
Initializing classes
function __construct ($host = ', $name = ', $pwd = ', $dBase = ') {
if ($host!= ')
$this->host = $host;
if ($name!= ')
$this->name = $name;
if ($pwd!= ')
$this->pwd = $pwd;
if ($dBase!= ')
$this->dbase = $dBase;
$this->init_conn ();
}
Link Database
function Init_conn () {
$this->conn= @mysql_connect ($this->host, $this->name, $this->pwd);
@mysql_select_db ($this->dbase, $this->conn);
mysql_query ("Set names UTF8");
}
Query results
function Mysql_query_rst ($sql) {
if ($this->conn = = ") {
$this->init_conn ();
}
$this->result = @mysql_query ($sql, $this->conn);
}

Get the number of query result fields
function Getfieldsnum ($sql) {
$this->mysql_query_rst ($sql);
$this->fieldsnum = @mysql_num_fields ($this->result);
}
Gets the number of query result rows
function Getrowsnum ($sql) {
$this->mysql_query_rst ($sql);
if (mysql_errno () = = 0) {
Return @mysql_num_rows ($this->result);
}else{
Return ";
}
}
Get an array of records indexed (single record)
function Getrowsrst ($sql) {
$this->mysql_query_rst ($sql);
if (mysql_error () = = 0) {
$this->rowsrst = mysql_fetch_array ($this->result,mysql_assoc);
return $this->rowsrst;
}else{
Return ";
}
}
Get an array of records indexed (multiple records) all
function Getrowsarray ($sql) {
$this->mysql_query_rst ($sql);
if (mysql_errno () = = 0) {
while ($row = mysql_fetch_array ($this->result,mysql_assoc)) {
$this->rowsarray[] = $row;
}
return $this->rowsarray;
}else{
Return ";
}
}
Update, delete, add records, return the number of rows affected
function Uidrst ($sql) {
if ($this->conn = = ") {
$this->init_conn ();
}
@mysql_query ($sql);
$this->rowsnum = @mysql_affected_rows ();
if (mysql_errno () = = 0) {
return $this->rowsnum;
}else{
Return ";
}
}
Gets the corresponding field value, a numeric index, and mysql_array_rows is the field index
function GetFields ($sql, $fields) {
$this->mysql_query_rst ($sql);
if (mysql_errno () = = 0) {
if (mysql_num_rows ($this->result) > 0) {
$TMPFLD = @mysql_fetch_row ($this->result);
$this->fields = $tmpfld [$fields];

}
return $this->fields;
}else{
Return ";
}
}

Error message
function Msg_error () {
if (Mysql_errno ()!= 0) {
$this->msg = Mysql_error ();
}
return $this->msg;
}
Releasing the result set
function Close_rst () {
Mysql_free_result ($this->result);
$this->msg = ';
$this->fieldsnum = 0;
$this->rowsnum = 0;
$this->filesarray = ';
$this->rowsarray = ';
$this->idsubtitle= ';
$this->idusername= ';
}
Close Database
function Close_conn () {
$this->close_rst ();
Mysql_close ($this->conn);
$this->conn = ';
}
}
?>

Use of instance 21 class, MD5 encryption of passwords
Copy CodeThe code is as follows:
<?php
$conne = new Opmysql ();
$conne-> Getrowsarray ($sql);
$conne-> close_conn ();
$password = "123,456,123,456";
echo MD5 ($password.) Www.kuphp.com ")//output to 32-bit ciphertext, is not decrypted function, can achieve simple encryption function.
?>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.