PHP MySQL Introduction
In this page:
$pdo = new PDO("mysql:host=localhost;dbname=database_name", "user", "password");
$conn = mysqli_connect("localhost", "user", "password", "database_name");
PDO से Connect करना
MySQL एक relational database है जो defined columns वाली structured tables में data store करता है, और PHP इससे connect करता है एक single page request से आगे किसी web application को चाहिए persistent data save और retrieve करने के लिए।
उदाहरण: Connecting with PDO
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
// Print "Table created -- structured rows and columns, just like MySQL" to the output
echo "Table created -- structured rows and columns, just like MySQL";
?>
Login to try C/C++/Java/PHP code in the editor
MySQLi से Connect करना
PHP MySQL से बात करने के दो main तरीके offer करता है: mysqli extension (MySQL Improved) और PDO (PHP Data Objects), PDO एक consistent API के through कई अलग database systems के साथ काम करने का extra benefit देते हुए।
उदाहरण: Connecting with MySQLi
<?php
// SQLite3 stands in for MySQL here -- on your own server: new mysqli(...) or new PDO(...)
$db = new SQLite3(':memory:');
echo "Connected (PDO offers one API across multiple database systems)";
?>
Login to try C/C++/Java/PHP code in the editor
Data को Safely Select करना
original mysql_* function family PHP 7 में पूरी तरह हटा दी गई थी, इसलिए किसी भी modern PHP code को mysqli या PDO इस्तेमाल करना चाहिए — किसी codebase में पुराने mysql_* functions मिलना एक strong signal है कि इसे update करने की ज़रूरत है।
उदाहरण: Selecting Data Safely
<?php
// The old mysql_connect()/mysql_query() functions were removed in PHP 7 -- use mysqli or PDO instead
$db = new SQLite3(':memory:');
echo "Using a modern database extension";
?>
Login to try C/C++/Java/PHP code in the editor
Data को Safely Insert करना
एक typical database interaction तीन steps follow करती है: एक connection खोलना, एक query execute करना (ideally parameters के साथ, raw string concatenation नहीं), और result process या close करना — एक pattern जिसे यह chapter mysqli और PDO दोनों के साथ चलता है।
उदाहरण: Inserting Data Safely
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
$db->exec("INSERT INTO users (name) VALUES ('Alice')");
// Declare `$result`, set to `$db->query("SELECT * FROM users")`
$result = $db->query("SELECT * FROM users");
// Print a human-readable dump of `$result->fetchArray()`
print_r($result->fetchArray());
?>
Login to try C/C++/Java/PHP code in the editor
Connections बंद करना
user input को query string में directly concatenate करके कभी एक SQL query मत बनाएँ, क्योंकि यह SQL injection का classic root cause है — prepared statements, इस chapter में बाद में covered, standard defense हैं।
उदाहरण: Closing Connections
<?php
$db = new SQLite3(':memory:');
$name = "Robert'); DROP TABLE users; --";
// Never do this: $db->exec("INSERT INTO users (name) VALUES ('$name')");
echo "Use a prepared statement instead of concatenating: $name";
?>
Login to try C/C++/Java/PHP code in the editor
- user input concatenate करके SQL बनाना, जो SQL injection की अनुमति देता है।
- PDO error mode को exceptions पर set न करना, ताकि errors silent रहें।
- connection को सही से close या reuse करना भूल जाना।
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