1

How can I easily add data from MySQL to a multidimensional array in PHP? I am making a search box and I want to store the first name, and the last name in the array.

Why does not this work.

$result = mysqli_query($db, $sql) or die(mysqli_error());

$data = array();

while ($row = mysqli_fetch_array($result)) {
    $data[$row['primary']] = $row['firstname'], $row['lastname'];
}
3
  • 2
    learn basic php. $data[$row['primary']] = array($row['firstname'], $row['lastname']);. And if all you wanted as the firstname/lastname, that's all you should have selected, and the lien woudl be $data[$row['primary']] = $row Commented May 27, 2014 at 21:44
  • Ahh, Thank you. Worked out very well. Commented May 27, 2014 at 21:57
  • 1
    You can add this as an answer if you want. Commented May 28, 2014 at 23:47

1 Answer 1

1
$data[$row['primary']] = array($row['firstname'], $row['lastname']);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.