I want to fetch all my data from a MySQL
Database using PHP
.
I've already written some code, but it only shows/fetch one record whilst there are multiple records in my database.
public function readMail($nik_request){
$stmt = $this->con->prepare("SELECT * FROM surat_request WHERE nik_request = ?");
$stmt->bind_param("s",$nik_request);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
I'm using fetch_assoc
to read out data from a specific row.
public function getMailByNikRequest($nik_request){
$stmt = $this->con->prepare("SELECT * FROM surat_request WHERE nik_request = ?");
$stmt->bind_param("s",$nik_request);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
<?php
include '../includes/DbOperations.php';
$response = array();
if($_SERVER['REQUEST_METHOD']=='POST'){
if(
isset($_POST['nik_request'])) {
$db = new DbOperations();
if($db->readMail($_POST['nik_request'])){
$user = $db->getMailByNikRequest($_POST['nik_request']);
$response['error'] = false;
$response['id'] = $user['id'];
$response['nik_request'] = $user['nik_request'];
$response['nik_surat'] = $user['nik_surat'];
$response['jenis_surat'] = $user['jenis_surat'];
$response['keperluan'] = $user['keperluan'];
$response['keterangan'] = $user['keterangan'];
$response['status'] = $user['status'];
}else{
$response['error'] = true;
$response['message'] = "Not Found";
}
}
}else{
$response['error'] = true;
$response['message'] = "Invalid Request";
}
echo json_encode($response);
I already run this code, it works but that's not like what I want.