How can you retrieve data from the mysql database using php?

Hello kartik,

 Actually there are many functions that  are available in PHP to retrieve the data from the MySQL database.

The Function are :mysqli_fetch_array(), mysqli_fetch_row(), mysqli_fetch_assoc(), mysqli_fetch_object().

Few functions are mentioned below:

mysqli_fetch_array() – It is used to fetch the records as a numeric array or an associative array.

Sample code:

// Associative or Numeric array

$result=mysqli_query($DBconnection,$query);

$row=mysqli_fetch_array($result,MYSQLI_ASSOC);

echo "Name is $row[0]

";

echo "Email is $row['email']

";

mysqli_fetch_row() – It is used to fetch the records in a numeric array.

Sample code:

//Numeric array

$result=mysqli_query($DBconnection,$query);

$row=mysqli_fetch_array($result);

printf ("%s %s\n",$row[0],$row[1]);

mysqli_fetch_assoc() – It is used to fetch the records in an associative array.

Sample code:

// Associative array

$result=mysqli_query($DBconnection,$query);

$row=mysqli_fetch_array($result);

printf ("%s %s\n",$row["name"],$row["email"]);

mysqli_fetch_object() – It is used to fetch the records as an object.

Sample code:

// Object

$result=mysqli_query($DBconnection,$query);

$row=mysqli_fetch_array($result);

printf ("%s %s\n",$row->name,$row->email);

There are many other function that can be used to retrieve data from database for different purpose according to the user use of data.

Thank you!

In this tutorial you'll learn how to select records from a MySQL table using PHP.

Selecting Data From Database Tables

So far you have learnt how to create database and table as well as inserting data. Now it's time to retrieve data what have inserted in the preceding tutorial. The SQL SELECT statement is used to select the records from database tables. Its basic syntax is as follows:

SELECT column1_name, column2_name, columnN_name FROM table_name;

Let's make a SQL query using the SELECT statement, after that we will execute this SQL query through passing it to the PHP mysqli_query() function to retrieve the table data.

Consider our persons database table has the following records:

+----+------------+-----------+----------------------+
| id | first_name | last_name | email                |
+----+------------+-----------+----------------------+
|  1 | Peter      | Parker    |  |
|  2 | John       | Rambo     |    |
|  3 | Clark      | Kent      |    |
|  4 | John       | Carter    |   |
|  5 | Harry      | Potter    |  |
+----+------------+-----------+----------------------+

The PHP code in the following example selects all the data stored in the persons table (using the asterisk character (*) in place of column name selects all the data in the table).

Example

Procedural Object Oriented PDO

Download

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "demo");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 
// Attempt select query execution
$sql = "SELECT * FROM persons";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>id</th>";
                echo "<th>first_name</th>";
                echo "<th>last_name</th>";
                echo "<th>email</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['first_name'] . "</td>";
                echo "<td>" . $row['last_name'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        // Free result set
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
 
// Close connection
mysqli_close($link);
?>
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$mysqli = new mysqli("localhost", "root", "", "demo");
 
// Check connection
if($mysqli === false){
    die("ERROR: Could not connect. " . $mysqli->connect_error);
}
 
// Attempt select query execution
$sql = "SELECT * FROM persons";
if($result = $mysqli->query($sql)){
    if($result->num_rows > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>id</th>";
                echo "<th>first_name</th>";
                echo "<th>last_name</th>";
                echo "<th>email</th>";
            echo "</tr>";
        while($row = $result->fetch_array()){
            echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['first_name'] . "</td>";
                echo "<td>" . $row['last_name'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        // Free result set
        $result->free();
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
 
// Close connection
$mysqli->close();
?>
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
try{
    $pdo = new PDO("mysql:host=localhost;dbname=demo", "root", "");
    // Set the PDO error mode to exception
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
    die("ERROR: Could not connect. " . $e->getMessage());
}
 
// Attempt select query execution
try{
    $sql = "SELECT * FROM persons";   
    $result = $pdo->query($sql);
    if($result->rowCount() > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>id</th>";
                echo "<th>first_name</th>";
                echo "<th>last_name</th>";
                echo "<th>email</th>";
            echo "</tr>";
        while($row = $result->fetch()){
            echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['first_name'] . "</td>";
                echo "<td>" . $row['last_name'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        // Free result set
        unset($result);
    } else{
        echo "No records matching your query were found.";
    }
} catch(PDOException $e){
    die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
 
// Close connection
unset($pdo);
?>

Explanation of Code (Procedural style)

In the example above, the data returned by the mysqli_query() function is stored in the $result variable. Each time mysqli_fetch_array() is invoked, it returns the next row from the result set as an array. The while loop is used to loops through all the rows in the result set. Finally the value of individual field can be accessed from the row either by passing the field index or field name to the $row variable like $row['id'] or $row[0], $row['first_name'] or $row[1], $row['last_name'] or $row[2], and $row['email'] or $row[3].

If you want to use the for loop you can obtain the loop counter value or the number of rows returned by the query by passing the $result variable to the mysqli_num_rows() function. This loop counter value determines how many times the loop should run.

How can we retrieve data from the tables in MySQL?

How to Retrieve Data From a Single Table.
SELECT – the columns in the result set..
FROM – names the base table(s) from which results will be retrieved..
WHERE – specifies any conditions for the results set (filter).
ORDER BY – sets how the result set will be ordered..
LIMIT – sets the number of rows to be returned..

How do you retrieve data from a database?

Fetch data from a database.
Start by creating a new app..
Add a Screen to your app. ... .
Add data sources to your app by referencing some Entities in the Manage Dependencies window (Ctrl+Q). ... .
Publish the app by clicking the 1-Click Publish button. ... .
It's time to load some data to the Screen..

How save and retrieve image from MySQL database in PHP?

Store Image File in Database (upload..
Check whether the user selects an image file to upload..
Retrieve the content of image file by the tmp_name using PHP file_get_contents() function..
Insert the binary content of the image in the database using PHP and MySQL..
Show the image uploading status to the user..

How can we retrieve insert and delete data in MySQL using PHP?

Use the following steps to select insert update delete record using PHP and MySQL:.
DB. PHP – Connecting Database..
Insert. PHP – Insert Records Into MySQL DB..
Select. php – Select Record From MySQL DB..
Update. php – Update Record Into MySQL DB..
Delete. php – Delete Record From MySQL DB..