Get data from the MySQL. PHP OOP and functional example open

Get data from the MySQL. PHP OOP and functional example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

OOP example:

//create a connection
$conn = mysqli_connect('localhost', 'root', 'password', 'testdb');

//write our SQL query to a variable $sql
$sql = "SELECT * FROM Users";

if($result = $conn->query($sql)){//connect to the base

    foreach($result as $row){//iterate elements
        $userid = $row["id"];
        $username = $row["name"];
        $userage = $row["age"];
    }
}

Get data from the database by functional method:

$conn = mysqli_connect('localhost', 'root', 'password', 'testdb');

$sql = "SELECT * FROM Users";

if($result = mysqli_query($conn, $sql)){

    foreach($result as $row){
         
        $userid = $row["id"];
        $username = $row["name"];
        $userage = $row["age"];
    }
}
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation