Browse By

PHP Database connectivity and it’s use in mysqli.

Spread the love

Before proceeding you must be knowing about the SQL Query.

select * from table_name is the basic query which can get all the value from table_name.

$serverName = "localhost";  //this remain as it is for the host.
$userName = "root";  // default name for local server
$password = "";   // Blank for local server
$dbName = "Database Name";
$conn = new mysqli($serverName, $userName, $password, $dbName);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
//This is All about select query which is capable for extracting all information from table.
$query = "SELECT * FROM Table_Name";
$result = $conn->query($query);
while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"];         

//Delete Query whose id is 1
$sql = "delete from Table_Name where id='1'";

//To insert into table
$sql = "insert into Table_Name (firstname, lastname, email) VALUES ('John', 'Doe', '')";
if ($conn->query($sql) === TRUE) {
    $last_id = $conn->insert_id; // to get last inserted ID from table
    echo "records Inserted successfully";
} else {
    echo "Error:" . $conn->error;

//Update data into table whose id is 1
$sql = "UPDATE Table_Name SET Name='Samuel' WHERE id=1";
if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;

$conn->close(); //this is to close connection.

You can use any function, limit or Order By in query according to requirement.

Leave a Reply

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