PHP Study Notes: Use mysqli to connect to the database and Study Notes mysqli

Source: Internet
Author: User

PHP Study Notes: Use mysqli to connect to the database and Study Notes mysqli

In the episode, I changed the data's my. ini encoding to UTF-8 at night, and the database never started. I can change it back to gbk, so I can tell you where the problem is.

Because it is a link to the database, there is nothing to explain, just go to the code.

<? Php/* Connect to a MySQL server to connect to The database server */$ link = mysqli_connect ('localhost',/* The host to Connect to connect to The MySQL address */'jian ', /* The user to connect as connection MySQL user name */'000000',/* The password to use to connect to MySQL password */'jian '); /* The default database to query Connection database name */if (! $ Link) {printf ("Can't connect to MySQL Server. Errorcode: % s", mysqli_connect_error (); exit;} else echo 'the database is connected! ';/* Close the connection to Close the connection */mysqli_close ($ link);?>

Here, jian is actually the administrator of a library named jian. You do not need to write the database administrator, in actual projects, we may obtain the database account, password, and host address of a database administrator.

Test results:

 

Original database table:

Operation 1: Query student IDs, classes, and scores with scores greater than 60

SELECT id,class,scores  FROM jian_scores WHERE scores>60

All code:

<? Php/* Connect to a MySQL server to connect to The database server */$ link = mysqli_connect ('localhost',/* The host to Connect to connect to The MySQL address */'jian ', /* The user to connect as connection MySQL user name */'000000',/* The password to use to connect to MySQL password */'jian '); /* The default database to query Connection database name */if (! $ Link) {printf ("Can't connect to MySQL Server. Errorcode: % s", mysqli_connect_error (); exit;} else echo 'the database is connected! '. "<Br/>"; if ($ result = mysqli_query ($ link, 'select id, class, scores FROM jian_scores WHERE scores> 60 ')) {echo ('Id class score '). "<br/>";/* Fetch the results of the query result returned */while ($ row = mysqli_fetch_assoc ($ result )) {echo $ row ['id'], "& nbsp;", $ row ['class'], "& nbsp;", $ row ['scores'], "<br/>"; // printf ("% s (% s)", $ row ['id'], $ row ['class'], $ row ['scores']);}/* Destroy the result set and free t He memory used for it end query releases memory */mysqli_free_result ($ result);}/* Close the connection to Close the connection */mysqli_close ($ link);?>

Result:

 

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.