PHP Connection Database

Source: Internet
Author: User

PHP Connection Database

1. Connect Database functions

Mysqli_connect ( hostname, username, password ) The return value is one of our connection objects if the connection fails the error returns false

2. Judging the error function

Mysqli_connect_error ( Connection object ) error message return error message

Mysqli_connect_errno ( Connection object ) error number 0 indicates successful connection without error

3. Select database Functions

mysqli_select_db ( Connection object, the name of the database to select ); If you choose to return successfully true otherwise return false

4. Select a character set

Mysqli_set_charset ( the Connection object, the character set to select ), or If the selection succeeds returns True False

5. preparing SQL Statements

6. sending SQL statements

Mysqli_query ( Connection object , s Q L statement to be sent ); successful getting an object failed to return false

7. Working with result sets

7.1 Gets the number of entries

a) mysqli_num_rows ( the sending of SQL successful objects ) is used to get the number of record bars in the query only Select statement uses

b) mysqli_affected_rows ( Connection object ) the number of affected rows in the previous MySQL operation is limited to Insert UPDATE DELETE operation using return value 1 number of successfully inserted bars -1 execution failed

7.2 getting the contents of a query result set

Mysqli_fetch_array ( sending Object ) Returns the result of the query as a mixed array, returning one

Mysqli_fetch_row ( sending Object ) returns The result of the query as an indexed array, returning one

Mysqli_fetch_assoc ( send Object ) returns The result of the query as a group of associative numbers returns one

7.3 When adding an operation we can get the ID of the last insert

MYSQLI_INSERT_ID ( Connection object ) returns the ID of the last insertion

8. Close the database

Mysqli_close ( Connection object )


PHP Kung fu Cheats day dragon Eight steps

1. Connect to the database

2. Judging errors

3. Select a database

4. Select a character Set

5. Preparing SQL statements

6. Sending SQL statements

7. Working with result sets

8. Close the database

1. Connect to the database

Mysqli_connect (' hostname ', ' username ', ' password ');

[Email protected]_connect (' localhost ', ' root ', ' 123456 ');

Var_dump ($link);

2. Judging errors

Mysqli_connect_error (Connection object) error message

Mysqli_connect_errno (Connection object) error number

echo Mysqli_connect_errno ($link);

echo Mysqli_connect_error ($link);

if (Mysqli_connect_errno ($link)) {

echo Mysqli_connect_error ($link); exit;

echo ' wrong reconnect '; exit;

}


3. Select a database

mysqli_select_db ($link, ' ss21 ');


4. Select a character Set

Mysqli_set_charset ($link, ' UTF8 ');



5. Preparing SQL statements

$sql = "Select id,name,sex,age,city from Info";

$sql = "INSERT into info (name) VALUES (NULL)";

6. Sending SQL statements

$result = Mysqli_query ($link, $sql);

7. Working with result sets

echo mysqli_num_rows ($result);

echo mysqli_affected_rows ($link);


8. Close the database

Mysqli_close ($link);


PHP Connection Database

Related Article

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.