All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Fetch Single Row Data From Database In PHP

Last Updated : Mar 11, 2024

How To Fetch Single Row Data From Database In PHP

In this article we will show you the solution of how to fetch single row data from database in PHP, here we are going to show you example with fetching single row data from database using mysqli_fetch_row() method.

The mysqli_fetch_row() method accepts a result object as a parameter retrieves the contents of its current row as an array of strings then printed by specifying column number.

Step By Step Guide On How To Fetch Single Row Data From Database In PHP :-

Here we defined database server details in mysqli_connect() method because it executes database connection i.e referred as $con variable and if they not executed then it throws error.

Using mysqli_query() method we selected all values in table ‘info’by select query with limit as ‘1’ and it result stored on variable ‘$qry’ then we using mysqli_fetch_row() method fetched single row data that is stored on variable ‘$row’ then we printing result of retrieved data on webpage.

<?php
$con=mysqli_connect("localhost","root","","dbase");
if(!$con){
    echo $con->connect_error;
}
$qry=mysqli_query($con,"SELECT * FROM info limit 1");
$row=mysqli_fetch_row($qry);
echo "Name : ".$row['1'];
echo "<br>";
echo "Password : ".$row['2'];
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we defined database server details for executes database connection using mysqli_connect() method then this connection referred by variable ‘$con’.
  4. Using if condition we checking whether database connection exists or not if they not present then it throws error on webpage.
  5. The mysqli_query() method will executes defined select query of ‘SELECT * FROM info limit 1’ with ‘limit 1 – refers contain single row data’ on database ‘dbase’ to collect single row data in table ‘info’ with the help of $con variable then result stored on variable ‘$qry’.
  6. Using ‘mysqli_fetch_row()’ method we retrieving first row data from table ‘info’ it is stored on variable ‘$row’.
  7. Then finally retrieved first row data printed by specifying column number within square brackets in $row variable on webpage. Here ‘$row['1'] – 1 refers first index of column value and $row['2'] – 2 refers second index of column value’.
  8. We can also print ‘0th index of column value’ it will prints ID of row data if you need then add ‘$row['0']’ value with string ‘ID:’.
  9. Another thing is modify it accordingly with your database and table names, otherwise we can’t get result and also you got result as first record what you had from your database table then change column name as per in your table.

Conclusion :-

In conclusion now we are able to know how to fetch single row data from database in php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser it will prints result of ‘Name : dharani, Password : dhanu@123’ on webpage.

I hope this article on how to fetch single row data from database in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪