Home > Database > Mysql Tutorial > php mysql connects to database instance_MySQL

php mysql connects to database instance_MySQL

WBOY
Release: 2016-11-30 23:59:38
Original
1304 people have browsed it

An aside, I changed the my.ini encoding of the data to utf-8 at night, and then the database could not be started. I just changed it back to gbk. If anyone knows, let me know what the problem is.

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

<&#63;php

/* Connect to a MySQL server 连接数据库服务器 */
$link = mysqli_connect(
  'localhost', /* The host to connect to 连接MySQL地址 */
  'jian',   /* The user to connect as 连接MySQL用户名 */
  '123456', /* The password to use 连接MySQL密码 */
  'jian');  /* The default database to query 连接数据库名称*/

if (!$link) {
  printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
  exit;
}else
  echo '数据库连接上了!';

/* Close the connection 关闭连接*/
mysqli_close($link);
&#63;>

Copy after login

The jian here is actually the administrator of one of the libraries named jian. There is no need to write the database administrator, because in actual projects, what we may get is the database account, password, and host address of a library 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 codes:

<&#63;php

/* Connect to a MySQL server 连接数据库服务器 */
$link = mysqli_connect(
  'localhost', /* The host to connect to 连接MySQL地址 */
  'jian',   /* The user to connect as 连接MySQL用户名 */
  '123456', /* The password to use 连接MySQL密码 */
  'jian');  /* The default database to query 连接数据库名称*/

if (!$link) {
  printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
  exit;
}else
  echo '数据库连接上了!'. "<br/>";

if ($result = mysqli_query($link, 'SELECT id,class,scores FROM jian_scores WHERE scores>60 ')) {

echo('id 班级 分数 '). "<br/>";

/* Fetch the results of the query 返回查询的结果 */
while( $row = mysqli_fetch_assoc($result) ){
  echo $row['id'], " ", $row['class'], " ", $row['scores'], "<br/>";
  // printf("%s (%s) ", $row['id'],$row['class'], $row['scores']);
}

/* Destroy the result set and free the memory used for it 结束查询释放内存 */
mysqli_free_result($result);
}


/* Close the connection 关闭连接*/
mysqli_close($link);
&#63;>

Copy after login

Result:

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template