zoukankan      html  css  js  c++  java
  • PHP学习笔记:用mysqli连接数据库

    小插曲,晚上把数据的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'], "&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);
    ?>

    结果:

     

  • 相关阅读:
    Hashed collections哈希集合
    《Python 学习手册4th》 第九章 元组、文件及其他
    《Python 学习手册4th》 第八章 列表与字典
    《Python 学习手册4th》 第七章 字符串
    <转>ERP的测试用例模板
    <转>如何测试大型ERP软件?
    《Python 学习手册4th》 第六章 动态类型简介
    《Python 学习手册4th》 第四章 介绍Python对象类型
    《Python CookBook2》 第四章 Python技巧
    《Python CookBook2》 第四章 Python技巧 对象拷贝 && 通过列表推导构建列表
  • 原文地址:https://www.cnblogs.com/jianqingwang/p/5890111.html
Copyright © 2011-2022 走看看