MySQL 連接

使用MySQL二進制文件的MySQL連接

您可以在命令提示符下使用mysql二進制文件建立MySQL數據庫。

例:這是一個從命令提示符連接到MySQL服務器的簡單示例

[root@host]# mysql -u root -p
Enter password:******

這將爲您提供mysql>命令提示符,您可以在其中執行任何SQL命令。以下是上述命令的結果,以下代碼塊顯示了以上代碼的結果

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.46 Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>


您可以隨時在mysql>提示符下使用exit命令從MySQL數據庫斷開連接。

mysql> exit
Bye

使用PHP腳本的MySQL連接

PHP提供mysqli_connect()函數來打開數據庫連接。該函數有六個參數,如果成功則返回MySQL鏈接標識符,如果失敗則返回FALSE。

您可以隨時使用另一個PHP函數mysqli_close()與MySQL數據庫斷開連接。該函數採用單個參數,該參數是 mysqli_connect() 函數返回的連接。

如果未指定資源,則關閉最後打開的數據庫。如果成功關閉連接,此函數將返回true,否則返回false。

嘗試以下示例連接到MySQL服務器

<html>
<head>
<title>Connecting MySQL Server</title>
</head>
<body>
<?php
$dbhost = 'mysql';
$dbuser = 'test';
$dbpass = '123456';
$dbname = 'test';
$conn = mysqli_connect(dbhost, dbuser, dbpass, $dbname);

if(! $conn ) {
die('Could not connect: ' . mysqli_error($conn));
}
echo 'Connected successfully';
mysqli_close($conn);
?>
</body>
</html>

相關頁面

MySQL 創建數據庫

MySQL 刪除數據庫





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章