PHP MYSQL : Select records

With the use of PHP you can execute MySQL SELECT query to fetch records on your databse. You have several ways on how to fetch records from your MySQL database.

You can use this following functions:

First one is mysql_fetch_array() which fetch a result row as an associative array, a numeric array, or both.

Second mysqli_fetch_assoc() which fetch a result row as an associative array



Example code on how to delete records using PHP in MYSQL database server.

<?php

// variables
$user = "localhost";
$username = "root";
$password = "password";

// connect to database
$db = new mysqli($user,$username,$password);
$db->select_db('your_databse');
        if (mysqli_connect_errno()) {
                die( "Connection failed..." );
        }

// select records
$query = ("SELECT id, username, firstname, lastname FROM tbl_users");
if ($rs = mysqli_query($db, $query)) {
    while ($row = mysqli_fetch_assoc($rs)) {
        $id                =    $row["id"];
        $username    =    $row["username"];
        $firstname    =     $row["firstname"];
        $lastname     =    $row["lastname "];
    }
    mysqli_free_result($rs);   
}

//outputs
echo "ID: ".$id."<br>";
echo "USERNAME: ".$username."<br>";
echo "FIRSTNAME: ".$firstname."<br>";
echo "PASSWORD: ".$lastname ."<br>";

?>

1 comments:

Anonymous said...

typo error
mysqli_free_result($rsDateTime);

myslqi_free_result($rs);