MySQL是一種開源的關系型數(shù)據(jù)庫管理系統(tǒng),是Web開發(fā)中最常用的數(shù)據(jù)庫之一。下面讓我們來學習一些MySQL的基礎知識。
首先,我們需要學習的是如何連接MySQL數(shù)據(jù)庫:
$servername = "localhost"; $username = "root"; $password = ""; $dbname = "myDB"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("連接失敗: " . $conn->connect_error); } echo "連接成功";
接下來,我們可以開始創(chuàng)建表格:
$sql = "CREATE TABLE MyGuests ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP )";
接著,我們可以開始向表格中插入數(shù)據(jù):
$sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')";
我們還可以從表格中查找數(shù)據(jù):
$sql = "SELECT id, firstname, lastname FROM MyGuests WHERE lastname='Doe'"; $result = $conn->query($sql); if ($result->num_rows >0) { while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 結果"; }
最后,我們還需要學習如何關閉MySQL數(shù)據(jù)庫的連接:
$conn->close();
以上就是MySQL初學的基礎知識,希望大家掌握好這些內(nèi)容,為后續(xù)的學習打下堅實的基礎。