Database query operation in php
Recommended related mysql video tutorials: "mysql tutorial"
* 1. Query Operation
* 2. Three functions involved:
* 2-1. mysqli_query($db, $sql): Execute SQL statement and return the result set
* 2-2. mysqli_errno($db): Returns the error code of the last function execution
* 2-3. mysqli_error($db): Returns the error message of the last function execution
* 2-4. mysqli_num_rows($res): Returns the number of records in the result set
* 2-5. mysqli_fetch_row($res): Returns a record in an index array
* 2-5. mysqli_fetch_assoc($res): Returns a record as an associative array
* 2-5. mysqli_fetch_array($res,MYSQLI_NUM): Returns a record as an index array
* 2-5. mysqli_fetch_array( $res,MYSQLI_ASSOC): Returns a record as an associative array
* 2-5. mysqli_fetch_array($res,MYSQLI_BOTH): Returns a record as an index and relational array
* 2-5 . mysqli_fetch_object($res): Return a record in object mode
* mysqli_free_result($result);
* 2-4. mysqli_close($db): Close the current data connection
* 3. Query steps:
* 3-1. Connect to the database
* 3-2. Prepare SQL statements
* 3-3. Execute query
* 3-4. Detection result
* 3-5. If it is select, return the result set
* 3-5-1. If it is insert/update /delete: Returns the number of affected records
* 3-6. Close the database connection
//1. Connect to the database, require is not a function, no need to add brackets after it
define ('DB_HOST', 'localhost'); define ('DB_USER', 'root'); define ('DB_PASS', 'root'); define ('DB_NAME', 'php'); define ('DB_CHAR', 'utf8'); $db = @mysqli_connect(DB_HOST, DB_USER, DB_PASS); if (mysqli_connect_errno($db)) { exit('连接失败'.mysqli_connect_error($db)); } mysqli_select_db($db, DB_NAME); mysqli_set_charset($db, DB_CHAR);
//2. Prepare SQL statement
$sql = "SELECT name,salary FROM staff"; $sql = "SELECT name AS 姓名,salary AS 工资 FROM staff WHERE age>90";
//3. Execute the query: the result set object will be returned if successful, false
$result = mysqli_query($db, $sql); var_dump($result);die();
//4. Detection results
//If the result set exists
if (false != $result) { //这是写只是语义性更强 //如果结果集中存在记录,至少有一条 // if (mysqli_num_rows($result) > 0) { //mysqli_affected_rows($db)也可以完成同样的检测工作,注意参数是连接对象$db,不是结果集对象 if (mysqli_affected_rows($db) > 0) { // echo '共计:'.mysqli_num_rows($result).'条记录<br>'; echo '共计:'.mysqli_affected_rows($db).'条记录~~<br>';
//5. Process the result set
//5-1: Get the index part
while($row = mysqli_fetch_array($result, MYSQLI_NUM)){
//Get the index part Shortcut method
while($row = mysqli_fetch_row($result)){
//5-2: Get the associated part
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
//Shortcut method to get the associated part
while($row = mysqli_fetch_assoc($result)){
//5-3: Index association
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
//5-4: The quick way to get the index association is not to pass in the second parameter, which is the default value
while($row = mysqli_fetch_array($result)){
//5-4: In the form of objects
while($row = mysqli_fetch_object($result)){ // var_export($row);//以字符串方式表示查询结查 //如果是对象,可以用指向符来访问 echo $row->name.'--'.$row->salary; echo '<hr>'; //以变量方式表示,表示的内容更加完整丰富 // var_dump($row); } } else { echo '没有符合条件的记录'; } //查询失败的处理方式 } else { //必须要用exit()或die()终止脚本执行,否则后面的语句还会执行并会报错 exit('查询失败'.mysqli_errno($db).':'.mysqli_error($db)); }
//5. Release the result set (only for select)
mysqli_free_result($result);
//6. Close the database connection
mysqli_close($db);