How to select one data row by id and display using PHP and MYSQL query?

4
(9.6K Views)

×

Please Login or Register to process.

I am working on a student management system. I want to fetch one by one data rows by id. If I select id 1, then 1 id row should be select. I want to get the specific data row by ID using PHP and MYSQL. Is it possible to select and display data by id?  The data should be fetched by the id and display. 

For example - 

id   name 

1    Mack 

2    John 

3     Sem 

If I select id 1, then a mack name should be displayed. Is it possible to fetch specific data row and display a single row in PHP? 

 

asked 04 Nov 2019 in PHP by arnabisaha(2.1K Points)

Share

2 Answers
answered 04 Nov 2019 by shakti(9.1K Points) (edited)
2

Yes, it is possible to select data by id. You can select the MYSQL database table row using the id. It's called specific data by the user. It selects the specific row by SQL query. 

The SQL query - 

SELECT name FROM `table_name` WHERE id=1;

SELECT name FROM `table_name` WHERE id=2;

SELECT name FROM `table_name` WHERE id=3;

You can select the name from the

SELECT * FROM `table_name` WHERE id=1;  

SELECT * FROM `table_name` WHERE id=2;  

SELECT * FROM `table_name` WHERE id=3;  

MYSQL table using a query. If you want to select complete row data by id, then use the SQL query.

The SQL query to select the entire row by id - 

 

The above SQL query helps to select the entire row data by id. 

If you want to display the entire data by id, then use the PHP GET method. The GET method holds the id by https URL. 

Select the data and display it in HTML like this - 

<?php
include_once("config.php");
$result = mysqli_query($mysqli, "SELECT * FROM table_name ORDER BY id DESC"); 
?>

<table>
<?php 
while($res = mysqli_fetch_array($result)) {

echo '<tr>'; 

echo "<td>".$res['name']."</td>";

echo"</td><td><a href="display.php?id=$res[id]">Display</a>";

echo '</tr>'; 

}
?>

</table> 

 

It selects the data and displays it into the HTML table. 

Now, you need to create another PHP to display the whole data by id. 

Create a display.php page. 

<?php
include_once("config.php");
$id = $_GET['id'];
$result = mysqli_query($mysqli, "SELECT * FROM tabel_name WHERE id=$id");
while($res = mysqli_fetch_array($result))
{
$name = $res['name'];

//you can display more row data by id 

}
?>
<?php echo $name; ?> 

It will display specific data by id. 

I have another reference for you. You need to learn it.

CRUD application in PHP  

 

 


Comment

answered 04 Nov 2019 by vishalrana1(7.3K Points)
1

Thank you for asking. Yes, it is absolutely right. You can select the data by id using PHP and MYSQL database. First of all, understand the point. You want to fetch the columns one by one. Yes, you can do it by specifying the id number. 

Create an MYSQL table and select the data by id. Every row contains a different id number.

You should make the id auto increment. 

You can use the SQL query like this - 

SELECT name FROM `student` WHERE id=2;

Here, 'student' is an MYSQL table name. You can define your own table name. It selects the specific table row id number 2. You can change the id number to display the specific row data. 

SELECT name FROM `student` WHERE id=3;

Now, select all data in the HTML table. It fetches the whole table data and displays using the while loop

Insert and fetch data in HTML table using PHP 

The table data will be fetched in an HTML table and you can create another PHP page to display the row data by id. 

Create a link in the HTML table to display the data by id on another PHP page. 

echo"</td><td><a href="show.php?id=$res[id]">View</a>";

Create another PHP file. 

show.php and GET the id by the GET method

<?php
$id = $_GET['id'];
$query= mysqli_query($mysqli, "SELECT * FROM student WHERE id=$id");
while($row = mysqli_fetch_array($query))
{
$name = $row['name'];
$id = $row['id'];
}
?>
<?php echo '<h2>'.$name.'<h2><br>'; ?> 
<?php echo '<h2>'.$id.'<h2><br>'; ?> 

It fetches the specific data by id. 

I have another reference for you. 

Update data by id using PHP and MYSQL database 

 


Comment

Related Questions :-

Your Answer

You can post your answer after login..