MySQL是一種關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),廣泛應(yīng)用于Web開發(fā)領(lǐng)域。以下是一些基本的MySQL代碼段示例。
// 連接數(shù)據(jù)庫(kù) $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 創(chuàng)建連接 $conn = new mysqli($servername, $username, $password, $dbname); // 檢查連接是否成功 if ($conn->connect_error) { die("連接失敗: " . $conn->connect_error); } echo "連接成功"; // 插入數(shù)據(jù) $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if ($conn->query($sql) === TRUE) { echo "新記錄插入成功"; } else { echo "Error: " . $sql . "
" . $conn->error; } // 查詢數(shù)據(jù) $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = $conn->query($sql); if ($result->num_rows >0) { // 輸出數(shù)據(jù) while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 結(jié)果"; } // 關(guān)閉連接 $conn->close();
以上是一些基本的MySQL代碼段示例,可以幫助開發(fā)人員更好地理解如何使用MySQL進(jìn)行數(shù)據(jù)管理。