PHP MySQL Select Data
In this page:
$result = mysqli_query($conn, "SELECT * FROM table_name");
mysqli_num_rows($result); // how many rows
while ($row = mysqli_fetch_assoc($result)) {
echo $row["column_name"];
}
Basic SELECT और Results Fetch करना
mysqli_query($conn, 'SELECT * FROM users') query चलाता है और एक result set object return करता है, जिसे mysqli_fetch_assoc() फिर एक बार में एक row को हर column के name से keyed एक associative array में convert करता है -- इस fetch call को loop करना हर matching row process करता है।
उदाहरण: Basic SELECT and Fetching Results
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (name TEXT)");
$db->exec("INSERT INTO users (name) VALUES ('Alice'), ('Bob')");
// Declare `$result`, set to `$db->query("SELECT * FROM users")`
$result = $db->query("SELECT * FROM users");
// Keep looping while `$row = $result->fetchArray(SQLITE3_ASSOC)` holds
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
// Print `$row['name'] . "\n"` to the output
echo $row['name'] . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
कितनी Rows Return हुईं Check करना
mysqli_num_rows($result) एक SELECT query द्वारा match की गई rows की total count return करता है, जो एक "no results found" message दिखाने, एक result count दिखाने, या यह decide करने के लिए उपयोगी है कि loop चलाना worth है या नहीं।
उदाहरण: Checking How Many Rows Were Returned
<?php
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (name TEXT)");
$db->exec("INSERT INTO users (name) VALUES ('Alice'), ('Bob')");
$result = $db->query("SELECT COUNT(*) as total FROM users"); // mysqli_num_rows() equivalent
print_r($result->fetchArray(SQLITE3_ASSOC));
?>
Login to try C/C++/Java/PHP code in the editor
एक Single Row Fetch करना
जब एक query से ज़्यादा से ज़्यादा एक row return होने की उम्मीद हो -- जैसे किसी specific user को उसकी unique ID से लुकअप करना -- एक single mysqli_fetch_assoc() call, बिना loop के, उस एक row को retrieve करने के लिए काफी है, या कुछ match न होने पर null।
उदाहरण: Fetching a Single Row
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER, name TEXT)");
$db->exec("INSERT INTO users VALUES (1, 'Alice')");
// Declare `$result`, set to `$db->query("SELECT * FROM users WHERE id = 1")`
$result = $db->query("SELECT * FROM users WHERE id = 1");
// Print a human-readable dump of `$result->fetchArray(SQLITE3_ASSOC)`
print_r($result->fetchArray(SQLITE3_ASSOC));
?>
Login to try C/C++/Java/PHP code in the editor
Results को Objects की तरह Fetch करना
mysqli_fetch_object($result) mysqli_fetch_assoc() जैसा काम करता है लेकिन हर row को एक associative array के बजाय एक plain object की तरह return करता है -- इसलिए $row["name"] के बजाय, आप $row->name access करते हैं, जो कुछ developers को ज़्यादा naturally पढ़ने लायक लगता है।
उदाहरण: Fetching Results as Objects
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (name TEXT)");
$pdo->exec("INSERT INTO users (name) VALUES ('Alice')");
$stmt = $pdo->query("SELECT * FROM users");
$row = $stmt->fetchObject(); // mysqli_fetch_object() equivalent
echo $row->name;
?>
Login to try C/C++/Java/PHP code in the editor
सभी Rows को एक Array में Fetch करना
mysqli_fetch_all($result, MYSQLI_ASSOC) एक result set से बाकी बची हर row एक साथ retrieve करता है, associative arrays के एक array की तरह -- तब convenient जब आपको एक बार में एक row fetch करने के बजाय पूरा dataset memory में एक single PHP array की तरह चाहिए।
उदाहरण: Fetching All Rows into an Array
<?php
$pdo = new PDO('sqlite::memory:');
$pdo->exec("CREATE TABLE users (name TEXT)");
$pdo->exec("INSERT INTO users (name) VALUES ('Alice'), ('Bob')");
$stmt = $pdo->query("SELECT * FROM users");
print_r($stmt->fetchAll(PDO::FETCH_ASSOC)); // mysqli_fetch_all($result, MYSQLI_ASSOC) equivalent
?>
Login to try C/C++/Java/PHP code in the editor
- एक query के कम से कम एक row return करने की उम्मीद मानने से पहले mysqli_num_rows() check करना भूल जाना, एक empty result से fetch करने की कोशिश में एक error का कारण बनते हुए।
- सिर्फ एक या दो specific columns actually चाहिए होने पर SELECT * से हर column select करना, ज़रूरत से ज़्यादा data retrieve और transfer करते हुए।
- mysqli_fetch_assoc() (column name से keyed एक associative array return करता है) को mysqli_fetch_row() (एक plain indexed array return करता है) से mix up करना, इस्तेमाल की गई fetch style के लिए गलत तरीके से results access करते हुए।
- SELECT columns FROM table matching rows retrieve करता है, mysqli_query() के through चलाया और एक fetch function से loop किया जाता है।
- mysqli_fetch_assoc() हर row को column name से keyed एक associative array की तरह return करता है, सबसे commonly इस्तेमाल होने वाली fetch style।
- mysqli_num_rows() आपको बताता है कि एक query ने कितनी rows return कीं, loop से पहले check करने या एक "no results" message दिखाने के लिए उपयोगी।
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