MySQLi With Prepared Statements For Beginners

Prepared statements may seem intimidating at first, but once you get hang of it, it’ll seem like second nature to you. The goal of this tutorial is to transform someone with little to no knowledge of prepared statements, into an expert. You may also like Mysql Database Connection in PHP.

Database Connection

<?php
    $db = new mysqli('host', 'UserName', 'Password', 'DatabaseName');
    if($db->connect_errno > 0)
    {
        die('Unable to connect to database [' . $db->connect_error . ']');
    }
?>

Select Query

$results = $db->query("SELECT id,name from emp");

Data fetch in Database

while($row = $result->fetch_assoc())
{
    echo $row['id'];
    echo $row['name'];
}

Now rows Count

<?php
echo 'Total results: ' . $result->num_rows;
?>

Leave a Reply

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