'PHP programming the fastest to understand' lecture 6: Mysql database operation

  • 2020-03-31 21:19:39
  • OfStack

The answer is to make it a class-a database class. Through the function of the secondary packaging, achieve a very good reuse. Include it when you need it.

Before we get to the PHP database, let's take a look at the main points of Mysql: you can use phpmyadmin to learn database operations.

In phpmyadmin, you will see that the encoding is all in utf-8.

Mysql database type is mainly: char (string fixed space, how big is how many Chinese characters), varchar (string variable space, how big is how many Chinese characters initialization), int, how is how many (integer, float (floats), timestamp (date, can immediately XuanJian created automatically, when the output is formatted date), text (text), Boolean (Boolean)

SUM() can be counted when writing SQL statements; Order by 'id' DESC LIMIT 10,10, etc.

Learn SQL statement in phpmyadmin add, delete, change and check on the line.

Instance 20 Mysql class
 
<?php 
class opmysql{ 
private $host = 'localhost'; //Server address
private $name = 'root'; //Login account
private $pwd = ''; //The login password
private $dBase = 'a0606123620'; //Database name
private $conn = ''; //Database linked resources
private $result = ''; //The result set
private $msg = ''; //Returns the result
private $fields; //Return to the field
private $fieldsNum = 0; //Number of fields returned
private $rowsNum = 0; //Number of results returned
private $rowsRst = ''; //Returns an array of fields for a single record
private $filesArray = array(); //Return array of fields
private $rowsArray = array(); //Returns an array of results
private $idusername=array(); 
private $idsubtitle=array(); 
//Initialize the class
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(); 
} 
//Linked 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"); 
} 
//The query results
function mysql_query_rst($sql){ 
if($this->conn == ''){ 
$this->init_conn(); 
} 
$this->result = @mysql_query($sql,$this->conn); 
} 

//Gets 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 rows for the query result
function getRowsNum($sql){ 
$this->mysql_query_rst($sql); 
if(mysql_errno() == 0){ 
return @mysql_num_rows($this->result); 
}else{ 
return ''; 
} 
} 
//Get records array with index (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 records array has indexes (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, and 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, mysql_array_rows is the one with 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 ''; 
} 
} 

//The error message
function msg_error(){ 
if(mysql_errno() != 0) { 
$this->msg = mysql_error(); 
} 
return $this->msg; 
} 
// The release of 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 the database
function close_conn(){ 
$this->close_rst(); 
mysql_close($this->conn); 
$this->conn = ''; 
} 
} 
?> 

Example of the use of class 21, password md5 encryption
 
<?php 
$conne = new opmysql(); 
$conne-> getRowsArray($sql); 
$conne-> close_conn(); 
$password= " 123456 One, two, three, four, five, six." ; 
echo md5($password. " www.kuphp.com " );//The output is 32 - bit ciphertext, there is no decryption function, can achieve a simple encryption function.
?> 

Related articles: