← Back to PHP Course | Chapter 11: Database | Lesson 1 of 21

PHP MySQL Introduction

MySQL tables का एक विशाल organized cabinet है जहाँ एक website अपनी information store करती है, students की एक school की list जैसे। PHP वह messenger है जो cabinet से data माँगता है या नई entries add करता है।
Syntax
php
$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
<?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";
?>

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
<?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)";
?>

Data को Safely Select करना

original mysql_* function family PHP 7 में पूरी तरह हटा दी गई थी, इसलिए किसी भी modern PHP code को mysqli या PDO इस्तेमाल करना चाहिए — किसी codebase में पुराने mysql_* functions मिलना एक strong signal है कि इसे update करने की ज़रूरत है।

उदाहरण: Selecting Data Safely

php
<?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";
?>

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
<?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());
?>

Connections बंद करना

user input को query string में directly concatenate करके कभी एक SQL query मत बनाएँ, क्योंकि यह SQL injection का classic root cause है — prepared statements, इस chapter में बाद में covered, standard defense हैं।

उदाहरण: Closing Connections

php
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. user input concatenate करके SQL बनाना, जो SQL injection की अनुमति देता है।
  2. PDO error mode को exceptions पर set न करना, ताकि errors silent रहें।
  3. connection को सही से close या reuse करना भूल जाना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.