PHP8---MySQL extension

Table of contents

1. Introduction to MySQL extension

Second, the basic operation of PHP and MySQL connection

(1) Preparations for MySQL connection

         (2) Insertion and deletion operations

3. Query operation

(1)mysqli_fetch_assoc

(2)mysqli_fetch_row

(3)mysqli_fetch_array

4. Related functions

(1) Relevant field information

(2) Other functions


1. Introduction to MySQL extension

I won't say much about the introduction of PHP and MySQL connection. Everyone knows that PHP, as a background language, must interact with the database. Otherwise, how can we make a so-called dynamic website? Here I mainly explain PHP8. Some extensions after PHP5 have changed. For example, the connection mysql_connect cannot be recognized in PHP8 at all. It can only be used in versions below PHP5. PHP8 connection uses mysqli_connect

Second, the basic operation of PHP and MySQL connection

(1) Preparations for MySQL connection

   The connection between PHP and MySQL is basically divided into four steps:

     (1) Connect to MySQL

     (2) Set the character set (what character set the client is currently executing the script to use)

     (3) Select the database

     (4) Close the connection to release resources

  Here I do a basic operation to familiarize myself with the sql statement to delete a row of data.

<?php
  //进行MySQL连接
  $link=mysqli_connect('localhost','用户名','密码','选择的数据库','3306') or die('数据库连接失败');
  //设置编码集
  mysqli_set_charset($link,'utf8');
  //进行MySQL语句
  if(mysqli_query($link,'delete from new_1 where id=1')){
    echo '成功';
  }
  else{
    echo '失败';
  }
?>

(2) Insertion and deletion operations

<?php
   header('Content-type:text/html;charset=utf-8');
   //进行MySQL连接
   $link=mysqli_connect('localhost','root','gq99668877','class_first','3306');
   //设置编码集
   mysqli_set_charset($link,'utf8');
   //sql语句
   $sql1="insert into new_1 value (null,'asd','abxc');";
   $sql2="delete from new_1 where name='asd'";
   $res=mysqli_query($link,$sql1);
   mysqli_query($link,$sql2);
?>

3. Query operation

The basic operation of the query is to get the number of rows in the result set, and then start to get it. Note that you get it row by row. If you want to get all of them, you can use a loop to get them. There are three functions to get the result set. as follows.

(1)mysqli_fetch_assoc

What he returns after getting the result set is an array, the index is the field name, and the array value is the data value

<?php
   header('Content-type:text/html;charset=utf-8');
   //进行MySQL连接
   $link=mysqli_connect('localhost','root','gq99668877','class_first','3306');
   $sql="select *from new_1";
   $res=mysqli_query($link,$sql);
   echo '<pre>';
   $arr=mysqli_fetch_assoc($res);
   print_r($arr);
?>

 

(2)mysqli_fetch_row

After getting the result set, he returns an array, the array subscript is the number 0, 1, 2 and so on, and the array value is the data value

<?php
   header('Content-type:text/html;charset=utf-8');
   //进行MySQL连接
   $link=mysqli_connect('localhost','root','gq99668877','class_first','3306');
   $sql="select *from new_1";
   $res=mysqli_query($link,$sql);
   echo '<pre>';
   $arr=mysqli_fetch_row($res);
   print_r($arr);
?>

 

 

 (3)mysqli_fetch_array

The difference between mysqli_fertch_array and the previous one is that it can have three forms. The form here means that it can also index numbers or fields like the previous two, and it can also implement both. That is, just add the second parameter. The second parameter is fixed and can only be selected from the following three.

MYSQLI_NUM: The array subscript is a number

MYSQLI_ASSOC: The array subscript is the field name =

MYSQLI_BOTH: Both of the first two types are available, and the array subscript has both numbers and field names

<?php
   header('Content-type:text/html;charset=utf-8');
   //进行MySQL连接
   $link=mysqli_connect('localhost','root','gq99668877','class_first','3306');
   $sql="select *from new_1";
   $res=mysqli_query($link,$sql);
   echo '<pre>';
   $arr1=mysqli_fetch_array($res,MYSQLI_NUM);
   $arr2=mysqli_fetch_array($res,MYSQLI_ASSOC);
   $arr3=mysqli_fetch_array($res,MYSQLI_BOTH);

   print_r($arr1);
   print_r($arr2);
   print_r($arr3);
?>

 

 

4. Related functions

(1) Relevant field information

 mysqli_num_fields: Get the number of fields, that is, know how many columns there are

mysqli_fetch_field_direct: Through the reference of this function, we can know the name of any column, table name, etc.

<?php
   header('Content-type:text/html;charset=utf-8');
   //进行MySQL连接
   $link=mysqli_connect('localhost','root','gq99668877','class_first','3306');
   //设置编码集
   mysqli_set_charset($link,'utf8');
   //sql语句
   $sql="select *from new_1";
   $res=mysqli_query($link,$sql);
   
   //得到字段数
   echo mysqli_num_fields($res);
   echo '<br/>';

   $f=mysqli_fetch_field_direct($res,0);
   //第一列的字段名
   echo $f->name;
   echo '<br/>';
   //知道表名
   echo $f->table;
?>

(2) Other functions

mysqli_insert_id: Get the id of the last inserted record and it is self-incrementing. 

 

<?php
   header('Content-type:text/html;charset=utf-8');
   //进行MySQL连接
   $link=mysqli_connect('localhost','root','gq99668877','class_first','3306');
   //设置编码集
   mysqli_set_charset($link,'utf8');
   //sql语句
   $sql1="insert into new_1 value (null,'asd','abxc');";
   mysqli_query($link,$sql1);
   echo mysqli_insert_id($link);
?>

 

Guess you like

Origin blog.csdn.net/gaoqiandr/article/details/128523897