Get the App
SLTechnology News&Howtos  ›  Database  › 

PHPl link database

Shulou Source: shulou.com Published: 2022-06-01 16:35:56 11月16日 Update

PHP connects to the database

1. Connect database function

The return value of mysqli_connect (hostname, username, password) is one of our connection objects. If the connection fails, an error report returns false.

two。 Judgment error function

Mysqli_connect_error (connection object) error message returns error message

Mysqli_connect_errno (connection object) error number 0 indicates a successful connection without errors

3. Select database function

Mysqli_select_db (connection object, database name to be selected); return true if the selection is successful, otherwise return false

4. Select character set

Mysqli_set_charset (connection object, character set to be selected); return true if the selection is successful, otherwise return false

5. Prepare sql statement

6. Send sql statement

Mysqli_query (connection object, sql statement to be sent); failure to get an object successfully returns false

7. Working with result set

7.1 get the number of entries

A) the number of records obtained by mysqli_num_rows (the object that sent sql successfully) can only be used by the select statement.

B) mysqli_affected_rows (connection object) the number of rows affected by the previous mysql operation is limited to the number of rows successfully inserted by the INSERT UPDATE DELETE operation with the return value of 1.

7.2 get the contents of the query result set

Mysqli_fetch_array (sending object) returns the results of the query as a mixed array, one at a time.

Mysqli_fetch_row (sending object) returns the results of the query as an indexed array, one at a time.

Mysqli_fetch_assoc (sending object) returns the results of the query as an associative array, one at a time.

7.3 when we add the operation, we can get the last inserted id.

Mysqli_insert_id (connection object) returns the last inserted id

8. Shut down the database

Mysqli_close (connection object)

=

The following are examples

/ / 1. Connect to the database

$link = mysqli_connect ('localhost','root','123456')

/ / 2. Error of judgment

If (mysqli_connect_errno ($link)) {

Echo mysqli_connect_error ($link); exit

}

/ / 3. Select a database

Mysqli_select_db ($link,'ss21')

/ / 4. Select character set

Mysqli_set_charset ($link,'utf8')

/ / 5. Prepare sql statement

$sql= "SELECT id,name,sex,age,city FROM info"

/ / 6. Send sql statement

$result = mysqli_query ($link,$sql)

/ / 7. Processing result

/ / var_dump (mysqli_fetch_array ($result))

/ / var_dump (mysqli_fetch_row ($result))

$user = array ()

While ($row=mysqli_fetch_assoc ($result)) {

/ / var_dump ($row)

$user [] = $row

}

Var_dump ($user)

/ / 8. Shut down the database

Mysqli_close ($link)

Tags: Objects data databases selections successes results statements errors queries functions characters character sets forms arrays information previous preparation processing host representative Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn Linux Shulou Information Xiaomi