Learn How To Get Single Record From Database In PHP

admin_img Posted By Bajarangi soft , Posted On 02-09-2020

Pull out Single record In MYSQL And Procedural WIth php

fetch single record in mysql php

First Need to Create Database:
 

Create database db_record;



Create Table:

CREATE TABLE `record` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(50) NULL DEFAULT NULL,
`last_name` VARCHAR(50) NULL DEFAULT NULL,
`email` VARCHAR(50) NULL DEFAULT NULL,
`password` text NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=12
;

 


You Need to Create HTML File
1. Add The HTML5 Doctype
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>

</body>
</html>

2. You Need to Add This Link Inside HTML File
<link rel="stylesheet" href="css/main.css">

3.Create a CSS File, Inside Your CSS Folder
main.css
table,th,td{
    border: 1px solid black;
    width: 1100px;
    background-color: lightblue;
}
.bs_btn{
    width: 20%;
    height: 5%;
    font-weight: bold;
}

4. You Need to Add PHP Code
<?php

$connection = mysqli_connect("localhost","root", "", "db_record");

$query = "SELECT * FROM record WHERE id='1'";
$query_run = mysqli_query($connection, $query);

$sql = mysqli_fetch_array($query_run);


//
?>


Complate Code Fetch Single record In MYSQL In php
Single_record.php
<?php

$connection = mysqli_connect("localhost","root", "", "db_record");

$query = "SELECT * FROM record WHERE id='1'";
$query_run = mysqli_query($connection, $query);

$sql = mysqli_fetch_array($query_run);


//
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap 4 Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container justify-content-center mt-5">
    <h3>How do I pull out a single record in MySQLi and in Procedural with PHP?</h3>
    <table class="table table-bordered mt-4">
        <thead>
            <th>First Name</th>
            <th>LAst Name</th>
            <th>Email</th>
        </thead>
        <tbody>
         <tr>
             <td><?php echo $sql['first_name'] ?></td>
             <td><?php echo $sql['last_name'] ?></td>
             <td><?php echo $sql['email'] ?></td>
         </tr>
        </tbody>
    </table>
</div>

</body>
</html>

Related Post