php mysql连接数据库实例
小插曲,晚上把数据的my.ini编码改为utf-8,然后数据库一直不能启动,改回gbk就可以,有知道的告知下问题所在。
因为是链接数据库,也没什么好说明的,直接上代码吧。
<?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的库的管理员,不需要写数据库管理员,因为实际项目我们可能得到的就是一个库管理员的数据库账号、密码、还有主机地址。
测试结果:
原来的数据库表:
操作1:查询分数大于60的学生id,班级和分数
SELECT id,class,scores FROM jian_scores WHERE scores>60
全部代码:
<?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); ?>
结果:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
详解oracle mysql 中的“不等于“ <> != ^=&nbs
oracle中的 != 与 <> 和 ^= 都是不等于,逻辑上没有区别而mysql中,<> 和 != 相同,接下来通过本文给大家详细讲解oracle mysql 中的“不等于“ <> != ^= is not,感兴趣的朋友一起看看吧2023-04-04mysql通过INSERT IGNORE INTO插入拼音字符无效问题及解决
这篇文章主要介绍了mysql通过INSERT IGNORE INTO插入拼音字符无效问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-08-08面试被问select......for update会锁表还是锁行
select … for update 是我们常用的对行加锁的一种方式,那么select......for update会锁表还是锁行,本文就详细的来介绍一下,感兴趣的可以了解一下2021-11-11
最新评论