Yuqoridagi ob'ektga yo'naltirilgan misolga e'tibor bering:
$connect_error PHP 5.2.9 va 5.3.0gacha buzildi. Agar siz 5.2.9 va 5.3.0 dan oldingi PHP versiyalari bilan mosligini ta'minlashingiz kerak bo'lsa, uning o'rniga quyidagi koddan foydalaning:
// Agar ulanishni
tekshiring (mysqli_connect_error()) {
die("Ma'lumotlar bazasiga ulanish muvaffaqiyatsiz tugadi: " . mysqli_connect_error());
}
Misol (MySQLi protsedurasi)
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Misol (PDO)
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Eslatma: Yuqoridagi PDO misolida biz ma'lumotlar bazasini (myDB) ham ko'rsatdik . PDO ulanish uchun tegishli ma'lumotlar bazasini talab qiladi. Agar ma'lumotlar bazasi ko'rsatilmagan bo'lsa, istisno chiqariladi.
Maslahat: PDO ning katta afzalligi shundaki, u bizning ma'lumotlar bazasi so'rovlarida yuzaga kelishi mumkin bo'lgan har qanday muammolarni hal qilish uchun istisno sinfiga ega. Agar try{ } blokida istisno qilinsa, skript ishlashni to‘xtatadi va to‘g‘ridan-to‘g‘ri birinchi catch(){ } blokiga o‘tadi.
Ulanishni yoping
Skript tugashi bilan ulanish avtomatik ravishda yopiladi. Avval ulanishni yopish uchun quyidagilarni foydalaning:
$conn→close();
MySQLi protsedurasi:
mysqli_close($conn);
PDO:
$conn = null;
MySQLi va PDO yordamida MySQL ma'lumotlar bazasini yarating
CREATE DATABASE operatori MySQL da ma'lumotlar bazasini yaratish uchun ishlatiladi.
Quyidagi misollar "myDB" nomli ma'lumotlar bazasini yaratadi:
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
Eslatma: Yangi ma'lumotlar bazasini yaratishda siz mysqli ob'ektiga faqat dastlabki uchta argumentni ko'rsatishingiz kerak (server nomi, foydalanuvchi nomi va parol).
Maslahat: Agar siz ma'lum bir portdan foydalanishingiz kerak bo'lsa, ma'lumotlar bazasi nomi argumenti uchun bo'sh qator qo'shing, masalan: new mysqli("localhost", "username", "password", "", port)
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE myDB";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Do'stlaringiz bilan baham: |