PHP connection to MySQL database two ways to encode
How PHP connects to MySQL database process-oriented coding style
<?php//One, process-oriented coding style//1.php connection to MySQL/* syntax Mysqli_connect (HOST,USERNAME,PA Sswd,dbname,port); The Mysqli_connect () method has a return value, which is the IP address of the computer on which PHP connects to the MySQL database *host-mysql the database on which it resides * username- Login MySQL Database user name * passwd-login MySQL database user password (write "" Stand if Password is empty) * dbname-Name of database to manipulate * The port number used by the Port-mysql database (generally default is 3306, not recommended) */$connect = mysqli_connect (' 127.0.0.1 ', ' root ', ', ' bigs Pinach ', ' 3306 '); 2.PHP sends SQL statements to the MySQL database and receives the returned results//2.1 write SQL statements/* * Add or delete operations returns a Boolean value * Add: INSERT into table name values (all field values) Note: The primary key self-increment field needs to be written as a NULL INSERT into table name field name = new Field value WH ERE Field name = field Value * Delete: Delete from table name delete from table name WHERE field name = field value * Change: UPDATE table name SET field name = field Value NOTE: This modification modifies the value of all the fields UPDATE table name SET field name = new Field value WHERE field name = field Value * Check operation return result set object (1) Basic query SELECT * FROM table name select field Name 1, field name 2,... From table name (2) Condition query SELECT * FROM table name WHERE field value = Field Name (3) Sort query select * FROM table name ORDER by field name Positive order: SELECT * From table name order BY field name ASC Reverse: SELECT * FROM table name order BY field name DESC (4) Fuzzy query SELECT * FROM table name WHERE field name like "string%" string% string (5) Limit (MySQL dialect) SELECT * from table name LIMIT [position offset,] row number; SELECT * FROM table name LIMIT 0, 5; Query first row of data start, show 5//Parse query result (Mysqli_result object) Mysqli_result Object Properties$field _count Number of fields * $num _rows How many data records * method * (1) Mysqli_fetch_array ($result, [result]); * $result-Result Set Object * result-resolves an array of returned arrays * A. Associative array mysqli _ASSOC; * b indexed array mysqli_num; * C. Two arrays are returned Mysqli_both (default Value) method $arr =new Array (); while ($row =mysqli_fetch_array ($result, num)) {Array_push ($arr, $row); * $arr: The array to press in * $row: Each loop gets Data Record} * (2) MYSQLI_FETCH_ASSOC ($ Result) The method returns the specific use of the indexed array method $arr =new Array (); while ($row =mysqli_fetch_assoc ($result)) {Array_push ($arr, $row); } */$sql = "Delete from Liukai";//delete data table named Liukai//2.2 solve Chinese garbled problem--fixed routine mysqli_query ($ Connect Connection object, ' SET NAMES UTF8 '); Mysqli_query ($connect, ' SET NAMES UTF8 '); 2.3 Send the SQL statement to the MySQL database and receive the results it returns $result = Mysqli_query ($connect, $sql); 3.PHP close connection to MySQL database Mysqli_close ($connect); ?>
Simplify the complex
<?php //1.建立连接 $connect=mysqli_connect(‘localhost‘,‘root‘,‘‘,‘bigspinach‘,‘3306‘); //2.定义sql语句 $sql=‘select * from liukai‘; mysqli_query($connect,‘set names utf8‘); //3.发送SQL语句 $result=mysqli_query($connect,$sql); $arr=array();//定义空数组 while($row =mysqli_fetch_array($result)){ //var_dump($row); //array_push(要存入的数组,要存的值) array_push($arr,$row); } var_dump($arr); //4.关闭连接 mysqli_close($connect); ?>
The coding style of the face object
<?php //面向对象的编码风格 //1.new一个 mysqli(host,username,passwd,dbname,port)对象 ====等同于建立连接 //2.定义sql语句 //3.调用mysqli 对象 的 query($sql)方法并得到其返回结果值 //4.调用mysqli 对象 的 close()方法关闭连接 $mysqli = new mysqli(‘127.0.0.1‘,‘root‘,‘‘,‘bigspinach‘,‘3306‘); $sql = "select * from where 姓名 = liukai "; $mysqli -> query(‘set names utf8‘); $mysqli -> query($sql); $mysqli -> close(); ?>
PHP connection MySQL Database