PHP MySQLi Connection
In this page:
$conn = mysqli_connect($host, $user, $password, $database); // procedural
$conn = new mysqli($host, $user, $password, $database); // object-oriented
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
mysqli_close($conn);
MySQLi क्या है?
mysqli_connect($host, $user, $password, $database) mysqli extension की procedural style इस्तेमाल करके MySQL server से एक connection खोलता है, एक connection object return करते हुए जिसे आप बाद के query functions को pass करेंगे।
उदाहरण: What is MySQLi?
<?php
// SQLite3 stands in for mysqli_connect() here -- on your own server:
// $conn = mysqli_connect("localhost", "root", "", "my_app");
$conn = new SQLite3(':memory:');
echo "Connection established";
?>
Login to try C/C++/Java/PHP code in the editor
Connection बंद करना
object-oriented style इसके बजाय एक new mysqli($host, $user, $password, $database) instance बनाती है, और ज़्यादातर modern PHP code इस style को prefer करता है क्योंकि यह दूसरे object-oriented database code के साथ ज़्यादा naturally पढ़ता है।
उदाहरण: Closing the Connection
<?php
// Object-oriented style: $conn = new mysqli($host, $user, $password, $database);
$conn = new SQLite3(':memory:');
echo get_class($conn);
?>
Login to try C/C++/Java/PHP code in the editor
Connection Errors Handle करना
connect होने के तुरंत बाद हमेशा connection में errors check करें, mysqli_connect_error() या object की ->connect_error property इस्तेमाल करके, ताकि एक गलत password या unreachable host बाद के confusing errors की एक cascade के बजाय एक clear message के साथ fail हो।
उदाहरण: Handling Connection Errors
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$conn`
$conn = new SQLite3(':memory:');
// Check whether `!$conn`
if (!$conn) {
// Print "Connection failed" to the output
echo "Connection failed";
// Otherwise, run this branch
} else {
// Print "Connected successfully" to the output
echo "Connected successfully";
}
?>
Login to try C/C++/Java/PHP code in the editor
एक Database Select करना
Connection credentials (host, username, password) को version control में committed किसी script में कभी directly hardcode नहीं किया जाना चाहिए — उन्हें environment variables या एक gitignored config file में store करना secrets को आपके repository से बाहर रखता है।
उदाहरण: Selecting a Database
<?php
// Declare `$host`, set to `getenv('DB_HOST') ?: 'localhost'`
$host = getenv('DB_HOST') ?: 'localhost';
// Declare `$user`, set to `getenv('DB_USER') ?: 'root'`
$user = getenv('DB_USER') ?: 'root';
// Print "Connecting to $host as $user (credentials from environment, not hardcoded)" to the output
echo "Connecting to $host as $user (credentials from environment, not hardcoded)";
?>
Login to try C/C++/Java/PHP code in the editor
एक Simple Query Execute करना
mysqli_close() या ->close() से explicitly एक connection close करना अच्छा practice है, हालाँकि PHP script के execution के अंत में automatically भी कोई खुले connections बंद कर देगा।
उदाहरण: Executing a Simple Query
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$conn`
$conn = new SQLite3(':memory:');
$conn->exec("CREATE TABLE test (id INTEGER)");
// Print "Query executed" to the output
echo "Query executed";
$conn->close();
// Print " -- connection closed" to the output
echo " -- connection closed";
?>
Login to try C/C++/Java/PHP code in the editor
- connect होने के बाद
$mysqli->connect_errorcheck न करना, ताकि एक failed connection ignore हो जाए और बाद की queries confusing errors के साथ fail हो जाएँ। - पुराने
mysql_*functions इस्तेमाल करना, जो PHP 7 में हटा दिए गए थे और अब exist नहीं करते। - publicly served script में database credentials hard-code करना बजाय उन्हें एक protected config file में रखने के।
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