MySQL是一種流行的關系型數據庫管理系統。Native MySQL是指使用MySQL標準協議和API操作MySQL數據庫。
//連接MySQL數據庫 $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully";
使用native MySQL可以執行各種數據庫操作,如查詢、插入、更新和刪除數據。
//查詢數據 $sql = "SELECT id, name, age FROM users"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) >0) { // 輸出數據 while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Age: " . $row["age"]. "
"; } } else { echo "0 results"; } //插入數據 $sql = "INSERT INTO users (name, age) VALUES ('John Doe', 30)"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "
" . mysqli_error($conn); } //更新數據 $sql = "UPDATE users SET age=35 WHERE id=1"; if (mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($conn); } //刪除數據 $sql = "DELETE FROM users WHERE id=3"; if (mysqli_query($conn, $sql)) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . mysqli_error($conn); }
總之,使用native MySQL可以輕松執行各種數據庫操作,并且可以提高應用程序的性能和可擴展性。