PHP笔记-连接MySQL数据库及查询数据

x33g5p2x  于2022-01-04 转载在 PHP  
字(3.6k)|赞(0)|评价(0)|浏览(273)

程序运行截图:

数据库内容:

要配置。我这是Windows的机器,修改php.ini

将此处放开即可。

程序结构:

list.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>列表</title>
</head>
<body>
    <table border="1">
        <tr>
            <td>ID</td>
            <td>内容</td>
            <td>时间</td>
        </tr>

        <?php foreach($testData as $item): ?>
        <tr>
            <td><?php echo $item["id"] ?></td>
            <td><?php echo $item["content"] ?></td>
            <td><?php echo $item["test_time"] ?></td>
        </tr>
        <?php endforeach; ?>

    </table>
</body>
</html>

list.php

<?php
    include_once "sql.php";
    $conn = connect("root", "123456", "phpTest", $error, "127.0.0.1", "3306");
    if(!$conn){

        die($error);
    }

    $sql = "select * from MyTest";
    $testData = read($conn, $sql, $error);
    if($error){

        die($error);
    }

//    print_r($testData);

    include "list.html"
?>

sql.php

<?php
    function connect($user, $password, $dbName, &$error, $host = "localhost", $port = "3306", $charset = "utf8"){

        $connection = @mysqli_connect($host, $user, $password, $dbName, $port);
        if(!$connection){

            $error = mysqli_connect_error();
            return false;
        }

        if(!mysqli_set_charset($connection, $charset)){

            $error = mysqli_error($connection);
            return false;
        }

        return $connection;
    }

    function read($conn, $sql, &$error){

        $res = query($conn, $sql, $error);
        if($res === false) return false;

        $lists = [];

        while($row = mysqli_fetch_assoc($res)){

            $lists[] = $row;
        }

        mysqli_free_result($res);
        return $lists;
}

    function query($conn, $sql, &$error){

        $res = mysqli_query($conn, $sql);
        if($res === false){

            $error = mysqli_error($conn);
            return false;
        }

        return $res;
    }
?>

需要注意的地方:

①list.php中的die

function die($status = "") void
Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.
die is a language construct and it can be called without parentheses if no status is passed.
Parameters:
int|string
$status
[optional]
If status is a string, this function prints the status just before exiting.
If status is an integer, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.
Note: PHP >= 4.2.0 does NOT print the status if it is an integer.

从中可以知道die($status)是终止函数并且释放$status。

②sql.php中的@mysqli_connect

/**
 * Open a new connection to the MySQL server
 * Alias of <b>mysqli::__construct</b>
 * @link https://php.net/manual/en/mysqli.construct.php
 * @param string $hostname Can be either a host name or an IP address. Passing the NULL value or the string "localhost" to this parameter, the local host is assumed. When possible, pipes will be used instead of the TCP/IP protocol.
 * @param string $username The MySQL user name.
 * @param string $password If not provided or NULL, the MySQL server will attempt to authenticate the user against those user records which have no password only.
 * @param string $database If provided will specify the default database to be used when performing queries.
 * @param int $port Specifies the port number to attempt to connect to the MySQL server.
 * @param string $socket Specifies the socket or named pipe that should be used.
 * @return mysqli|false object which represents the connection to a MySQL Server or false if an error occurred.
 */
function mysqli_connect ($hostname = null, $username = null, $password = null, $database = null, $port = null, $socket = null) {}

从中可知,创建一个新连接去连接MySQL。

③sql.php中的mysql_connect_error():

从中可知返回最后连接错误的string字符串。

④sql.php中的mysql_set_charset():

从中可知是设置客户端连接时的字符集。并且有返回值,设置成功和失败。

⑤sql.php中的mysqli_fetch_assoc():

从中可知,从查询到的结果集获取一行作为关联数组,如果给完了返回值为null。

⑥sql.php中的mysqli_query

从中可知,是用来检索数据的。

相关文章

微信公众号

最新文章

更多