关键词搜索

源码搜索 ×
×

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

发布2022-01-03浏览2173次

详情内容

程序运行截图:

数据库内容:

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

将此处放开即可。

程序结构:

list.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>列表</title>
  6. </head>
  7. <body>
  8. <table border="1">
  9. <tr>
  10. <td>ID</td>
  11. <td>内容</td>
  12. <td>时间</td>
  13. </tr>
  14. <?php foreach($testData as $item): ?>
  15. <tr>
  16. <td><?php echo $item["id"] ?></td>
  17. <td><?php echo $item["content"] ?></td>
  18. <td><?php echo $item["test_time"] ?></td>
  19. </tr>
  20. <?php endforeach; ?>
  21. </table>
  22. </body>
  23. </html>

 list.php

  1. <?php
  2. include_once "sql.php";
  3. $conn = connect("root", "123456", "phpTest", $error, "127.0.0.1", "3306");
  4. if(!$conn){
  5. die($error);
  6. }
  7. $sql = "select * from MyTest";
  8. $testData = read($conn, $sql, $error);
  9. if($error){
  10. die($error);
  11. }
  12. // print_r($testData);
  13. include "list.html"
  14. ?>

sql.php

  1. <?php
  2. function connect($user, $password, $dbName, &$error, $host = "localhost", $port = "3306", $charset = "utf8"){
  3. $connection = @mysqli_connect($host, $user, $password, $dbName, $port);
  4. if(!$connection){
  5. $error = mysqli_connect_error();
  6. return false;
  7. }
  8. if(!mysqli_set_charset($connection, $charset)){
  9. $error = mysqli_error($connection);
  10. return false;
  11. }
  12. return $connection;
  13. }
  14. function read($conn, $sql, &$error){
  15. $res = query($conn, $sql, $error);
  16. if($res === false) return false;
  17. $lists = [];
  18. while($row = mysqli_fetch_assoc($res)){
  19. $lists[] = $row;
  20. }
  21. mysqli_free_result($res);
  22. return $lists;
  23. }
  24. function query($conn, $sql, &$error){
  25. $res = mysqli_query($conn, $sql);
  26. if($res === false){
  27. $error = mysqli_error($conn);
  28. return false;
  29. }
  30. return $res;
  31. }
  32. ?>

 需要注意的地方:

①list.php中的die

  1. function die($status = "") void
  2. Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.
  3. die is a language construct and it can be called without parentheses if no status is passed.
  4. Parameters:
  5. int|string
  6. $status
  7. [optional]
  8. If status is a string, this function prints the status just before exiting.
  9. 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.
  10. Note: PHP >= 4.2.0 does NOT print the status if it is an integer.

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

②sql.php中的@mysqli_connect

  1. /**
  2. * Open a new connection to the MySQL server
  3. * Alias of <b>mysqli::__construct</b>
  4. * @link https://php.net/manual/en/mysqli.construct.php
  5. * @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.
  6. * @param string $username The MySQL user name.
  7. * @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.
  8. * @param string $database If provided will specify the default database to be used when performing queries.
  9. * @param int $port Specifies the port number to attempt to connect to the MySQL server.
  10. * @param string $socket Specifies the socket or named pipe that should be used.
  11. * @return mysqli|false object which represents the connection to a MySQL Server or false if an error occurred.
  12. */
  13. 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

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

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载