
因为是链接数据库,也没什么好说明的,直接上代码吧。
<?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 '数据库连接上了!'. "
";
if ($result = mysqli_query($link, 'SELECT id,class,scores FROM jian_scores WHERE scores>60 ')) {
echo('id 班级 分数 '). "
";
/* Fetch the results of the query 返回查询的结果 */
while( $row = mysqli_fetch_assoc($result) ){
 echo $row['id'], " ", $row['class'], " ", $row['scores'], "
";
 // 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);
?>
结果:

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
