PHP MySQL Create Or Delete Database In Hindi

📔 : PHP 🔗

पिछले topic में आपने सीखा कि कैसे PHP में MySQL database connect करते हैं and कैसे connection close करते हैं।
इस topic में आप सीखेंगे कि database connect होने के बाद किस तरह से database create या remove करते हैं।

PHP MySQL Create Database

File : php_mysql_createdb.php

Copy Fullscreen Close Fullscreen
<?php
$connection = mysqli_connect('localhost', 'root', null);
if(! $connection)
die('Database connection error : '.mysqli_connect_error());

/* write query to create a new database */
$query = 'create database db_test';
if(mysqli_query($connection, $query))
echo 'Database created successfully.';
else
echo 'Error : '.mysqli_error($connection);
?>
Output
Database created successfully .

Explain

  1. Example में die() function का use script die / stop करने के लिए किया गया है , जिससे कि database से connect न होने पर इससे आगे की script Run न हो सके।

  2. mysqli_query() का use MySQL Database के लिए query run के लिए किया जाता है , successful run होने पर true return करता है otherwise false.


इस program को रन करने के बाद आप अपने MySQL या phpmyadmin में check करेंगे तो एक new database db_test मिलेगा।

PHP MySQL Show All Databases

बैसे तो आप MySQL या phpmyadmin में सभी databases देख सकते हैं , लेकिन अगर PHP की help से सभी databases देखना चाहते हैं तो उसके लिए show databases query Run कर सकते हैं।

File : php_mysql_listdb.php

Copy Fullscreen Close Fullscreen
<?php
$connection = mysqli_connect('localhost', 'root', null);
if(! $connection)
die('Database connection error : '.mysqli_connect_error());

$result = mysqli_query($connection, 'show databases');
/*use mysqli_fetch_all() to get all data at once, that returns double dimensional array of data*/
$all_dbs = mysqli_fetch_all($result);
foreach ($all_dbs as $index => $db_row)
echo $db_row[0].'<br>';
?>
Output
db_test
information_schema
mysql
performance_schema
phpmyadmin
test

जब हमें database से data fetch करते हैं तो वहां पर mysqli_fetch_all() function का use करते हैं जो Rows का Double Dimensional Array return करता है, जिस पर for Loop या foreach Loop का use करके आसानी से iterate हैं । जैसा कि Output में आप देख सकते हैं।

PHP MySQL Delete Database

PHP में database delete करने के लिए बाकी queries की तरह ही delete database की query Run करनी होती है , But किसी particular database  delete करने के लिए सबसे पहले उसे select करना होता है।

Database को आप दो तरह से select कर सकते हैं -

  1. connect करते समय आप database का name दे सकते हैं।

  2. अगर connect करते समय database का name नहीं दिया है तो फिर mysqli_select_db(mysqli_link, db_name) function की help से select करना होता है।

Example :

File : php_mysql_deldb.php

Copy Fullscreen Close Fullscreen
<?php
$connection = mysqli_connect('localhost', 'root', null);
if(! $connection)
die('Database connection error : '.mysqli_connect_error());

/*select database*/
mysqli_select_db($connection, 'db_test');
if(mysqli_query($connection, 'drop database db_test'))
echo 'Database deleted.';
else
echo 'Error : '.mysqli_error($connection);
?>
Output
Database deleted.

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers