Tuesday, 25 October 2016

Select Statement and Select Data from MYSQLi

Assalamualaikum, untuk post kali ni saya akan beri kod untuk `select data` menggunakan MYSQLi.

Simple dan Mudah.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "DB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$year = date('Y');
$sql = "SELECT tr_facsurvey_1.percentage, tr_facsurvey_1.id_facsurvey1
FROM tr_facsurvey_1
JOIN lt_facilities ON tr_facsurvey_1.facilitycode = lt_facilities.facilitycode AND lt_facilities.statusMO = 'Y' AND lt_facilities.progKkawanku = 'Y'
WHERE tr_facsurvey_1.percentage >= '80' AND YEAR(tr_facsurvey_1.dtkeyin) = '$year'";
$result = $conn->query($sql);

$total= $result->num_rows;

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $percent = number_format(($row["percentage"]/$total)*100,2);
        echo "percentage: " . $row["percentage"]. " percent - " . $percent."<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

Sekian, Terima Kasih. Diharap kod ini dapat membantu anda. Semoga Berjaya. 

Writer :
25/10/2016

0 comments:

Post a Comment