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: