PHP学习笔记:用mysqli链接数据库

小插曲,晚上把数据的my.ini编码改成utf-8,而后数据库一直不能启动,改回gbk就能够,有知道的告知下问题所在。php

由于是连接数据库,也没什么好说明的,直接上代码吧。mysql

<?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); ?>

这里的jian实际上是其中一个名字为jian的库的管理员,不须要写数据库管理员,由于实际项目咱们可能获得的就是一个库管理员的数据库帐号、密码、还有主机地址。sql

测试结果:数据库

 

原来的数据库表:服务器

操做1:查询分数大于60的学生id,班级和分数测试

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

所有代码:fetch

<?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'], "&nbsp;", $row['class'], "&nbsp;",  $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); ?>

结果:编码

相关文章
相关标签/搜索