PHP connects to the database in two ways-oriented to process-oriented object

Source: Internet
Author: User

First, object-oriented
1. Link Database
$conn = @new mysqli ("127.0.0.1", "Root", "" "," mydb ");

if ($conn->connect_errno) {//Return link error number
Return link error message
Die ("Database link failed:". $conn->connect_error);
}

2. Select Database
$conn->select_db ("MyDB") or Die ("Select Database failed:". $conn->error);

3. Set the character set encoding
$conn->set_charset ("UTF8") or Die ("set character Set failed:". $conn->error);

4. Prepare the SQL statement
$sql = <<<sql
Select * from TB1;
sql

5. Execute SQL statement, return result set or Boolean type True/false
$res = $conn->query ($sql);

6. Returns the number of fields in the result set
Var_dump ($res->field_count);
    Returns the total number of rows in the result set
Var_dump ($res->num_rows);

7. Returns an associative array and an indexed array
Var_dump ($res->fetch_array ());
    Returns an indexed array
Var_dump ($res->fetch_row ());
    Returns the associative array
Var_dump ($res->fetch_assoc ());
    Returns an object
Var_dump ($res->fetch_object ());

8. Move the result set pointer to the specified position
$res->data_seek (0);


9. Returns a field in the result set and moves the pointer to the next column
Var_dump ($res->fetch_field ());
    Directly returns all fields in the result set
Var_dump ($res->fetch_fields ());


10. Release the result set resource
$res->close ();//$res->free ();
    Close database connection
$conn->close ();


Second, process-oriented

1. Connect to the database

@ $conn = Mysqli_connect ("127.0.0.1", "root", "slk5550123", "mydb");

if (Mysqli_connect_error ($conn)) {//Return error message
Exit ("Database connection failed, failure number". Mysqli_connect_errno ($conn). " Failure message: ". Mysqli_connect_error ($conn));
}

Connect to the database and judge
@ $conn = Mysqli_connect ("127.0.0.1", "root", "slk55501233", "mydb") or Die ("database connection failed, failure message:". Mysqli_connect_error ($conn ));


Connection database: Mysqli_connect
Parameters: ① host address ②mysql user name ③mysql password ④ Select the linked database ⑤ port number
Returns: If the link succeeds, returns the identity symbol for the resource type
Returns False if the connection fails

If there is more than one connection to MySQL, then the various functions that operate the database later must pass in the returned connection symbol.
If there is only one connection to MySQL, then the various functions of the database will not have to pass in this sign


3. Select Database mysqli_select_db
Parameter: ① resource designator ② selected database name
Return: The connection succeeded returns True, the connection failed to return false

If you modify the database successfully, the database in the resource identifier is changed
If the modification fails without terminating the operation through the code, subsequent code can continue execution using the original database.

Select the database and judge
mysqli_select_db ($conn, "mydb") or Die ("Database selection failed");

4. Setting Character Set encoding
Mysqli_set_charset ($conn, "UTF8");
Can only be utf8, but not make utf-

5. Writing SQL statements
$sql = "SELECT * from TB1";

6. Execute SQL statements
If additions and deletions are changed, the bool type is returned to indicate whether the DML was successful
If it is a query, the resource result set is returned DQL
Returns False if the query fails


$res = Mysqli_query ($conn, $sql); $res = Mysqli_query ($conn, $sql);

7. Returns the number of rows affected by the operation in the database and the ID of the INSERT statement

DML: The number of rows affected when the last action is returned
Var_dump (Mysqli_affected_rows ($conn));
Returns the last newly inserted primary key ID when an INSERT statement is executed
Var_dump (musqli_insert_id ($conn))

Returns the number of rows in a resource result set when DQL
Mysqli_num_rows ($res);
When DQL, returns the number of fields in the resource result set
Mysqli_num_fields ($res);

8. Working with result sets, returning associative arrays and indexed arrays
Parameter: ① The result set to be processed
② returns that array format, MYSQLI_ASSOC-associative array
Mysql_num Array of numbers
Mysql_both default, simultaneous generation of associative and numeric arrays


Returns an associative array
MYSQLI_FETCH_ASSOC ($result)
Returns an indexed array
Mysqli_fetch_row ($result)
Returns an object
Mysqli_fetch_object ($result)

9. Return a field in the result set and move the pointer to the next column
Mysqli_data_seek: Setting the result set pointer position
Mysqli_data_seek ($res, 0);

Returns information for each column field in the result set (field name, table name, database name, field type, length, and so on) */
Mysqli_fetch_field ($res);


10. Release the query resource result set and close the database connection
Mysql_free_result ($res);
To close a database connection
Mysql_close ($conn);

PHP connects to the database in two ways-oriented to process-oriented object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.