Home  >  Article  >  Backend Development  >  Android programmers learn PHP development (27)-Database exercises-PhpStorm

Android programmers learn PHP development (27)-Database exercises-PhpStorm

黄舟
黄舟Original
2017-03-03 09:55:061377browse

Since it is a database exercise, you first create a database, let’s create one:

1. Enter phpmyadmin and create a new database:


2. Create a new data table:


3. Create a new field: (select utf8_bin for collation)


4. Check A_I and let the id increase automatically:


5. Save:


6. Try to insert data:


7. Write some information and click to execute:


8. Click to browse and take a look at the effect:


9. The insertion is successful, indicating that there is no problem with our table. The id is also self-increasing:


The following is the source code:

<?php
    /**
     * 数据库练习
     * http://www.php.cn/
     *
     * mysql_connect — 打开一个到 MySQL 服务器的连接
     * mysql_error — 返回上一个 MySQL 操作产生的文本错误信息
     *
     * mysql_query — 发送一条 MySQL 查询
     *
     * mysql_select_db — 选择 MySQL 数据库
     *
     * mysql_get_client_info — 取得 MySQL 客户端信息
     * mysql_get_host_info — 取得 MySQL 主机信息
     * mysql_get_proto_info — 取得 MySQL 协议信息
     * mysql_get_server_info — 取得 MySQL 服务器信息
     * mysql_num_rows — 取得结果集中行的数目
     *
     * mysql_field_name — 取得结果中指定字段的字段名
     * mysql_num_fields — 取得结果集中字段的数目
     *
     * strtoupper() 函数把字符串转换为大写。
     *
     * mysql_close — 关闭 MySQL 连接
     */

    // step 1 : 连接数据库(返回资源)
    $link = mysql_connect("localhost","root","");
    var_dump($link); // 打印结果:resource(3) of type (mysql link)
    echo "<br>";
    if (!$link){
        echo "连接数据库失败<br>错误信息:";
        echo mysql_error();
    }

    /**
     * step 2 : 设置操作
     */
    //mysql_query("set names utf8"); // 设置字符集,但是不建议使用,影响效率!
    // 文本编辑器、数据库 都使用utf8 , 这里就不需要设置字符集

    /**
     * step 3 : 选择一个数据库作为默认的数据库使用
     */
    mysql_select_db("iwh");

    /**
     * step 4 : 操作数据库的SQL语句执行
     * 执行语句分为两种:
     * 1、没有结果,返回真假
     * 2、有结果,执行成功 返回结果集(资源),处理资源,从结果集中 将结果取出并格式化处理
     * 没有结果集的语句:DML DCL DDL  create insert update delete
     */
    echo mysql_get_client_info()."<br>"; // mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $
    echo mysql_get_host_info()."<br>"; // localhost via TCP/IP
    echo mysql_get_proto_info()."<br>"; // 10
    echo mysql_get_server_info()."<br>"; // 5.5.5-10.1.19-MariaDB

    //$sql = "create table bro_users(id int)"; // 创建表
    //$sql = "insert into bro_users values(1)"; // 插入1
    //$sql = "desc users"; // 查询表
    //$sql = "select * from users"; // 查询表
    //$sql = "show tables"; // 查询表

    //$sql = "select * from users"; // 查询表
    //$sql = "desc users"; // 查询表结构
    $sql = "select id,name,nickname,email from users";
    echo "SQL:{$sql}<br>";

    // http://www.php.cn/:8888/myPhpDemo/mysqlDemo.php?name=liudehua&password=123456&nickname=huazai&email=hua@qq.com
    //$sql = "insert into users(name, password, nickname, email) values(&#39;{$_GET[&#39;name&#39;]}&#39;,&#39;{$_GET[&#39;password&#39;]}&#39;,
    //    &#39;{$_GET[&#39;nickname&#39;]}&#39;,&#39;{$_GET[&#39;email&#39;]}&#39;)"; // bool(true)
    // 可以在http://www.php.cn/:8888/phpmyadmin/sql.php?db=iwh&table=users查看插入结果

    // 可以影响行数的函数(判断表是否有变化)
    //echo mysql_num_rows();

    $result = mysql_query($sql); // 只要放一个正确的sql就可以执行
    var_dump($result); // 如果语句正确,打印:bool(true) ;如果语句错误,打印:bool(false)
                        // 如果是  desc users  ,打印结果集,打印:resource(4) of type (mysql result)
                        // 如果是  select * from users  ,打印结果集,打印:resource(4) of type (mysql result)
    echo "<br>";


    /**
     * 从结果集的资源中,获取我们想要的结果,按我们的方式或格式显示
     * mysql_fetch_row — 从结果集中取得一行作为枚举数组
     * mysql_fetch_assoc — 从结果集中取得一行作为关联数组
     * mysql_fetch_array — 从结果集中取得一行作为关联数组,或数字数组,或二者兼有
     * mysql_fetch_object — 从结果集中取得一行作为对象
     *
     * 以上命令默认指针指向第一条数据,每次调用都向后移动一条数据,有数据返回true,没有数据返回false
     */
//    while ($arr = mysql_fetch_assoc($result)){
//        print_r($arr);
//        echo "<br>";
//    }
//    echo "---------- ---------- ----------<br>";
//
//    print_r(mysql_fetch_row($result));
//    echo "<br>";
//
//    print_r(mysql_fetch_assoc($result));
//    echo "<br>";
//
//    print_r(mysql_fetch_array($result));
//    echo "<br>";
//
//    print_r(mysql_fetch_object($result));
//    echo "<br>";

    echo &#39;<table border="1" width="800" align="center">&#39;;

    // 遍历字段
    echo &#39;<tr>&#39;;
    for ($i=0;$i<mysql_num_fields($result);$i++){
        echo &#39;<th>&#39;.mysql_field_name($result, $i)."</th>>";
    }
    echo &#39;</tr>&#39;;

    while ($row = mysql_fetch_assoc($result)){ // 遍历表内容
        echo &#39;<tr>&#39;;
        foreach ($row as $col){
            echo &#39;<td>&#39;.$col.&#39; </td>&#39;;
        }
        echo &#39;</tr>&#39;;
    }
    echo &#39;</table>&#39;;

    echo "共有".mysql_num_rows($result)."条记录!<br>";
    echo "共有".mysql_num_fields($result)."个字段!<br>";




    //----------下面的是推荐写法----------这样写法更加规范灵活
    $result = mysql_query($sql); // 只要放一个正确的sql就可以执行

    echo &#39;<table border="1" width="800" align="center">&#39;;

    // select id,name,nickname,email from users 如果使用这边写法,则固定了字段名可以自己直接输出,而不用查询
    echo &#39;<tr>&#39;;
    echo &#39;<th>id</th>>&#39;;
    echo &#39;<th>name</th>>&#39;;
    echo &#39;<th>nickname</th>>&#39;;
    echo &#39;<th>email</th>>&#39;;
    echo &#39;</tr>&#39;;

    while ($row = mysql_fetch_assoc($result)){ // 遍历表内容
        echo &#39;<tr>&#39;;
//        foreach ($row as $col){
//            echo &#39;<td>&#39;.$col.&#39; </td>&#39;;
//        }
        echo "<td>{$row[&#39;id&#39;]}</td>";
//        echo "<td>{$row[&#39;name&#39;]}</td>";
        echo "<td>".strtoupper($row[&#39;name&#39;])."</td>";
        echo "<td>{$row[&#39;nickname&#39;]}</td>";
        echo "<td>{$row[&#39;email&#39;]}</td>";
        echo &#39;</tr>&#39;;
    }
    echo &#39;</table>&#39;;

    echo "共有".mysql_num_rows($result)."条记录!<br>";
    echo "共有".mysql_num_fields($result)."个字段!<br>";

    //----------下面的是推荐写法 2 ----------
    $result = mysql_query($sql); // 只要放一个正确的sql就可以执行

    echo &#39;<table border="1" width="800" align="center">&#39;;

    // select id,name,nickname,email from users 如果使用这边写法,则固定了字段名可以自己直接输出,而不用查询
    echo &#39;<tr>&#39;;
    echo &#39;<th>id</th>>&#39;;
    echo &#39;<th>name</th>>&#39;;
    echo &#39;<th>nickname</th>>&#39;;
    echo &#39;<th>email</th>>&#39;;
    echo &#39;</tr>&#39;;

    while (list($id, $name, $nickname, $email) = mysql_fetch_row($result)){ // 遍历表内容
        echo &#39;<tr>&#39;;
        echo "<td>{$id}</td>";
        echo "<td>{$name}</td>";
        echo "<td>{$nickname}</td>";
        echo "<td>{$email}</td>";
    }
    echo &#39;</table>&#39;;

    echo "共有".mysql_num_rows($result)."条记录!<br>";
    echo "共有".mysql_num_fields($result)."个字段!<br>";

    /**
     * step N : 关闭连接
     */
    mysql_close();

The above is Android programmers learning PHP development (27)-database exercise- The content of PhpStorm, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Statement:
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