No Results showing from MySQL Query [closed]

I have a table named “repairs”. Inside that table I have the following rows:

job_number (INT11) AUTO INCREMENT -> this is the number that the customer enters into the repair status form I am building…

Date | client_full_name | item_for_repair | tech_assigned | technician_notes | current_status

The whole thing is supposed to first check if the job number the customer enters into the form is in the database and show the status of that job number based on what’s in the database. If the job number they enter does not exist in the database, or they enter it wrong, they are prompted with a No Record Found message…

<?php

if (isset($_POST['btn_get_status'])) {
    // Connection code.
    $options = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];
    $pdo = new PDO('mysql:host=localhost;port=3306;dbname=ecemscoz_ecemsapp;charset=utf8mb4', 'ecemscoz_ecemsapp', '[email protected]', $options);

    // Prepared statement
    $stmt = $pdo->prepare('SELECT * FROM repairs WHERE job_number=?');
    $stmt->execute([$_POST['job_number']]);
    $exists = $stmt->fetchColumn(); // either 1 or null

    if ($exists) {
        echo "Status for $job_number is currently $current_status";
    } else {
        echo 'No Record Found';
    }
}

The issue I am now facing is that when I enter a number that’s NOT in the databse, it returns the else{ statement of No Record Found…. PERFECT!

No Record Found Working As Expected:
No Record Found Working As Expected

But if I enter a job number that does really exist, it shows blank fields where job number and current status need to be.

Result is Empty:
Result is Empty

I am really struggling to understand what I am missing or where to implement it. Is my new PDO code an improvement or has it got worse with this try?

Answer

The validation is in the query itself:

$stmt = $pdo->prepare('SELECT * FROM repairs WHERE job_number=?');

If that returns any result then it exists. You should change the $exists = $stmt->fetchColumn(); for $exists = $stmt->fetch();

With that exists will have two posible states: 1) an empty array if there is no job. 2) an array with all the rows and columns matching that job number. Then you can do something like

if (empty($exists)) {
    echo 'No Record Found';
} else {
    echo "Status for {$exists['job_number']} is currently {$exists['current_status']}";
}

As @Dharman says you could reverse the if:

if ($exists) {
    echo "Status for {$exists['job_number']} is currently {$exists['current_status']}";
} else {
    echo 'No Record Found';
}