Table of Contents
Introduction
हेलो दोस्तों इस post tutorial में php का इस्तेमाल करके database को कैसे connecting कर सकते है ये सब हम जानेगे!. mysql database के tables के data को access करने से पहले हमें php और database के बीच में successfully connection करना होता है!. और पहले तो हमें ये पता होना चाहिए की ये database क्या होता है!. और ये sql क्या होती है और इसके इस्तेमाल क्या है तो इस सबके बारेमे हम जानेगे इस post में.
What is a Database और what is SQL?
Database: systematic data का collection है. Database जो होता है वह multiple tables का collection होता है. और related tables के अनुसार data table में row और column के formated में store होते है!. Database को एक example से समझते है!. जब हम पहली बार facebook में account creates करते है तब हम अपना FirstName, LastName, Email, Mobile, Profile जैसी information देते है तो वह बी किसी एक database के table में ही store हुवे होंगे.
SQL: एक structure query language है! जो database के साथ interact यानि communicate करने के लिए इस्तेमाल किया जाता है!. sql का इस्तेमाल करके हम database में data add करने के लिए, database से data को get करने के लिए, database के exiting data को update करने के लिए और data को delete करने के लिए sql structured query language से कर सकते है!.
How to connecting Database
Database को हम दो तरीके से connect कर सकते है!.
- MySQLi extension.
- PDO
MySQLi extension
Mysqli एक open-source relational database management system है जिसका इस्तेमाल websites और webapplication में data को manage करने के लिए किया जाता है!. Mysqli खास करके mysql database के साथ connect होता है और इसके साथ काम करता है. यह server-side prepared statement का support करता है. और वह client-side prepared statement का support नहीं करता.
Database connect करने के लिए mysqli_connect() इस function का इस्तेमाल किया जाता है | इस function के parameter के रूप में servername , username, password, database name को set करना होता है!.
mysqli_connect($servername, $username, $password,$database);
Example:
<?php
// Connecting to the Database
$servername = "localhost";
$username = "root";
$password = "";
$database = "databasename";
// Create a connection
$connection = mysqli_connect($servername, $username, $password, $database);
// Die if connection was not successful
if ( $connection ){
echo "Connection was successful";
}else{
die("Sorry we failed to connect: ". mysqli_connect_error());
}
?>
PDO (PHP Data Object)
Php Data Object एक php extension है जो database के साथ connection आसान तरीके से करता है!. PDO 12 अलग-अलग database systems के साथ काम कर सकता है!. अगर हमारा project किसी दुसकरे database में सेट करना पद जाये तो PDO इस process को आसान तरीके से करने में मदद करता है!. hame फ़क्त connection string और थोड़ी सी query की changing करना पड़ता है!.
new PDO("mysql:host=servername;dbname=database", "username", "password");
Example:
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$database = 'databasename';
try{
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
echo "Connected successfully.";
}
catch(PDOException $e){
echo "Connection failed : " . $e->getMessage();
}
?>
Close Database Connection
mysqli और PDO का connection close करने के लिए जरुरी statement वैसे तो connection close करने की जरुरत नहीं होती है क्यों की जब php script close होती है तो वह automatically close हो जाते है!.
MySQLi Object-Oriented:
$conn->close();
PDO:
$conn = null;






