PHP笔记-连接MySQL数据库及查询数据
生活随笔
收集整理的這篇文章主要介紹了
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数据库及查询数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于java的科研信息管理平台
- 下一篇: java初始化数据报_java – 如