PHP MySQL Create DB
In this page:
$sql = "CREATE DATABASE IF NOT EXISTS database_name";
if (!mysqli_query($conn, $sql)) {
echo "Error: " . mysqli_error($conn);
}
CREATE DATABASE से एक Database बनाना
MySQL से एक connection जो कोई particular database specify नहीं करता फिर भी एक बिल्कुल नई CREATE DATABASE statement चला सकता है -- mysqli_query($conn, "CREATE DATABASE dbName") वह SQL command भेजता है और अगर connecting user के पास permission हो तो database बना देता है।
उदाहरण: Creating a Database with CREATE DATABASE
<?php
// mysqli_query($conn, "CREATE DATABASE dbName") on a real MySQL server
$db = new SQLite3('app.db');
echo "Database file created";
?>
Login to try C/C++/Java/PHP code in the editor
IF NOT EXISTS से Errors से बचना
CREATE DATABASE IF NOT EXISTS dbName statement को एक से ज़्यादा बार चलाने के लिए safe बनाता है -- database पहले से exist करने पर एक error raise करने के बजाय, MySQL बस कुछ नहीं करता और script को continue करने देता है, जो कई बार चल सकने वाली setup scripts के लिए उपयोगी है।
उदाहरण: Avoiding Errors with IF NOT EXISTS
<?php
// CREATE DATABASE IF NOT EXISTS dbName -- safe to run more than once
$db = new SQLite3('app.db'); // creates the file only if it doesn't already exist
echo "Ready, whether or not it already existed";
?>
Login to try C/C++/Java/PHP code in the editor
Creation Errors Check करना
mysqli_error($conn) उस connection पर सबसे हाल के error का एक description return करता है -- एक CREATE DATABASE call (या किसी query) के बाद इसे check करना आपको specifically बताता है कि कोई operation क्यों fail हुआ, जैसे insufficient privileges या एक invalid database name।
उदाहरण: Checking for Creation Errors
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE test (id INTEGER)");
// Check whether `$db->lastErrorCode()`
if ($db->lastErrorCode()) {
// Print `"Error: " . $db->lastErrorMsg()` to the output
echo "Error: " . $db->lastErrorMsg();
// Otherwise, run this branch
} else {
// Print "No errors" to the output
echo "No errors";
}
?>
Login to try C/C++/Java/PHP code in the editor
Create करने के बाद एक Database Select करना
एक database बनाना automatically इसे current connection के लिए active नहीं बना देता -- या तो एक अलग USE dbName statement चलाएँ, या एक fresh connection खोलें जो database को सीधे इसके चौथे mysqli_connect() argument की तरह नाम दे।
उदाहरण: Selecting a Database After Creating It
<?php
// USE dbName; -- or pass the db name as mysqli_connect()'s 4th argument
$db = new SQLite3('app.db'); // SQLite selects the file directly on connect
echo "Active database selected";
?>
Login to try C/C++/Java/PHP code in the editor
एक Database के लिए Character Set और Collation चुनना
CREATE DATABASE dbName CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci creation time पर database के लिए default character encoding और sorting rules set करता है -- utf8mb4 modern recommended choice है क्योंकि यह emoji सहित Unicode की पूरी range support करता है।
उदाहरण: Choosing Character Set and Collation for a Database
<?php
// CREATE DATABASE dbName CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
echo "utf8mb4 supports the full Unicode range, including emoji";
?>
Login to try C/C++/Java/PHP code in the editor
- पहले से exist करने वाली किसी database के same name से एक database बनाने की कोशिश करना, जो एक error raise करता है जब तक आप specifically SQL में IF NOT EXISTS से check न करें।
- पर्याप्त privileges न रखने वाले connection से CREATE DATABASE चलाना, क्योंकि इसे elevated database-creation rights चाहिए जो everyday application user के पास अक्सर नहीं होते।
- यह भूल जाना कि एक database बनाना automatically इसे select नहीं करता -- आपको अभी भी उस database को specify करने वाला एक अलग USE statement या एक fresh connection चाहिए।
- CREATE DATABASE dbName एक नई, empty database बनाता है, एक मौजूदा connection पर mysqli_query() के through चलाया जाता है।
- किसी database को बनाने के लिए इस्तेमाल किया गया connection पहले किसी particular database को select करने की ज़रूरत नहीं रखता, क्योंकि CREATE DATABASE server level पर operate करता है।
- IF NOT EXISTS CREATE DATABASE statement को अगर database पहले से exist करे तो error दिए बिना दोबारा चलाने के लिए safe बनाता है।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- PHP MySQL Introduction
- PHP MySQLi Connection
- PHP PDO Introduction
- PHP CRUD Operations
- PHP Prepared Statements
- PHP Stored Procedures
- PHP Transactions
- PHP Error Handling in DB
- PHP MySQL Connect
- PHP MySQL Create DB
- PHP MySQL Create Table
- PHP MySQL Insert Data
- PHP MySQL Get Last ID
- PHP MySQL Insert Multiple
- PHP MySQL Prepared Statements
- PHP MySQL Select Data
- PHP MySQL Where
- PHP MySQL Order By
- PHP MySQL Delete Data
- PHP MySQL Update Data
- PHP MySQL Limit Data