PHP MySQL Select Data In Hindi

📔 : PHP 🔗

पिछले topic में आपने MySQL Table में records को insert करना सीखा , इस topic में आप सीखेंगे कि Table से कैसे data को print करना है।

PHP MySQL Select All Data

Table से data fetch करे के लिए हमें MySQL Query run करनी पड़ती है , और जब भी हम Table से records fetch करते हैं तो हमेशा हमें एक Two Dimensional Array मिलता है।

File : php_mysql_select.php

Copy Fullscreen Close Fullscreen
<?php
$connection = mysqli_connect("localhost", "root", null, "db_test");
if(! $connection)
die("Database connection error : ".mysqli_connect_error());

$query = "SELECT * FROM tbl_users";
$rows = mysqli_fetch_all( mysqli_query($connection, $query), MYSQLI_ASSOC );
/*now it's time to print it in a table*/
$table = "<table border='1'>
<thead>
<tr>
<th>ID</th> <th>First Name</th> <th>Last Name</th>
<th>Email</th> <th>About User</th> <th>Create Date</th>
</tr>
</thead>";
if(count($rows) > 0)
{
foreach ($rows as $row)
{
$table .= "<tr>
<td>".$row['id']."</td> <td>".$row['first_name']."</td><td>".$row['last_name']."</td>
<td>".$row['email']."</td><td>".$row['about_user']."</td><td>".$row['create_date']."</td>
</tr>";
}
}
else
{
$table .= "<tr><td>Data not found</td></tr>";
}
$table .= '</table>';
echo $table;
?>
Output
ID First Name Last Name Email About User Create Date
1 RahulKumar info@learnhindituts.comI am a student2021-01-11 09:25:48
2 RajuVerma raju@gmail.com2021-01-11 10:01:47
3 RajuVerma raju@gmail.com2021-01-11 10:07:04
4 MohanLal mohan@gmail.comhi this is Mohan Lal2021-01-11 10:07:04

Exaplain:

  1. Example में data fetch करने के लिए mysqli_fetch_all() function का use किया गया है जो कि सभी rows को एक साथ return करता है। इस Function में दो argument pass किये गए हैं। mysqli_query() result & एक predefined keyword MYSQLI_ASSOC .

  2. बैसे तो MYSQLI_ASSOC optional होता है , अगर आप इसे pass नहीं करते हैं तो हमें Tables का Indexed Array मिलता है। जबकि MYSQLI_ASSOC pass करने पर हमें Associative Array मिलता है जिससे कि Column values को Column name से access कर सकते हैं। जैसा कि Example में दिखाया गया है।

  3. कुछ इस तरह Table से data fetch करते है , अब इस Two Dimensional Associative Array पर किसी भी तरह के Loop for Loop / foreach Loop या while Loop apply कर सकते हैं।


हालाँकि यह जरूरी नहीं है कि आप हर बार सभी rows एक बार में ही select करें need के according कभी - कभी एक single record की भी जरूरत पड़ जाती है। वहां पर हम MySQL limit use करते हैं।
For Example :

File : php_mysql_select.php

Copy Fullscreen Close Fullscreen
<?php
$connection = mysqli_connect("localhost", "root", null, "db_test");
if(! $connection)
die("Database connection error : ".mysqli_connect_error());

$query = "SELECT * FROM tbl_users LIMIT 1";
$row = mysqli_fetch_all( mysqli_query($connection, $query), MYSQLI_ASSOC );
echo '<pre>';
print_r($row);
?>
Output
Array
(
    [0] => Array
        (
            [id] => 1
            [first_name] => Rahul
            [last_name] => Kumar
            [email] => info@learnhindituts.com
            [about_user] => I am a student
            [create_date] => 2021-01-11 09:25:48
        )

) 

I hope अब आपको समझ आ गया होगा कि PHP में MySQL Table से कैसे data को fetch करते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers