Assign key name based on row while loop Is it possible to automate the array inside the while loop and automatically assign the key name based on the row so I don't have to spell out each one?

$stmt = $conn-&gt;prepare("SELECT * FROM tickets_info WHERE ticket = ?");
$stmt-&gt;execute(array($_POST['ticket']));

if ($stmt-&gt;rowCount()) {

    while ($row = $stmt-&gt;fetch()) {

        $ticket = [
            'name'      =&gt; $row['name'],
            'company'   =&gt; $row['company'],
            'email'     =&gt; $row['email'],
            'phone'     =&gt; $row['phone'],
            'address'   =&gt; $row['address'],
        ];

    }

}
There is no identifiable difference between your table's column names and your preferred keys, but if you want to alter the keys, just build that into your SELECT clause -- this is what "aliases" are for.

Suppose you wanted to capitalize each column name, here's how that could look:

$stmt = $conn-&gt;prepare("SELECT name AS Name,
                               company AS Company,
                               email AS Email,
                               phone AS Phone,
                               address AS Address
                        FROM tickets_info
                        WHERE ticket = ?");
$stmt-&gt;execute([$_POST['ticket']]);
$ticket = $stmt-&gt;fetch(PDO::FETCH_ASSOC) ?: [];


Note, I am not using a loop.  Your snippet is overwriting itself in the loop, so I am interpreting that to mean that ticket is a PRIMARY/UNIQUE column.



If you want to extract all columns from your table and use the column names as keys, you don't need to change your original query.

$stmt = $conn-&gt;prepare("SELECT * FROM tickets_info WHERE ticket = ?");
$stmt-&gt;execute([$_POST['ticket']]);
$ticket = $stmt-&gt;fetch(PDO::FETCH_ASSOC) ?: [];