Boxecutor Boxecutor - 1 year ago 49
MySQL Question

how do i fetch a mysql record then assign each field to different variables

I have a MySQL table with various fields

firstname
,
lastname
,
email
,
phone
etc... Each one has a unique id.

How can I use PHP to retrieve the fields then assign them each to a separate PHP variable for example when it gets the user record by using the id it then assigns
firstname
to
$firstname
,
lastname
to
$lastname
etc...

Here is what I have tried so far:

$sql = "SELECT * FROM $table
WHERE 'id' = $id";
$result = $mysqli->query($sql);

while ($row = mysqli_fetch_assoc($result)){
$firstname = $row['firstname']; $lastname = $row['lastname'];
//and so on for other fields
}

Answer Source

hope this helps.

<?php

$query = "select * from some_table where id=some_id";

$exe = mysqli_query($db, $query);

while($row = mysqli_fetch_assoc($exe))
{
  $field1 = $row['field1'];
  $field2 = $row['field2'];
  $field3 = $row['field3'];
  $field4 = $row['field4'];
  $field5 = $row['field5'];
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download