0

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.

  • If it works, what's the problem with it? If you expect anything else to happen, can you explain **what** you expect? – Nico Haase Feb 15 '23 at 09:05
  • Hello @NicoHaase, the problem is the code only read one row of my data not all of them, I have more than one row – MuhammadAl Feb 15 '23 at 09:08
  • Please add all clarification to your question by editing it. "read one row of my data" - where does this happen? What have you tried to resolve that? – Nico Haase Feb 15 '23 at 09:30

0 Answers0