日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發布時間:2025/3/15 数据库 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP笔记-连接MySQL数据库及查询数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

程序運行截圖:

數據庫內容:

要配置。我這是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

<?phpinclude_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

<?phpfunction 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

?從中可知,是用來檢索數據的。

總結

以上是生活随笔為你收集整理的PHP笔记-连接MySQL数据库及查询数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。