how to create array from MYSQL answer in PHP?

i have code like that:


try {
  $polaczenie = @new mysqli($host, $db_user, $db_password, $db_name);
  if ($polaczenie->connect_errno!=0) {
    throw new Exception(mysqli_connect_errno());
  } else {
  $result = $polaczenie->query("SELECT * FROM `miasta`");
  if (!$result) {
    throw new Exception($polaczenie->error);
    } else {
catch (Exception $e) {
echo "Server error";

my MYSQL table looks like this:

enter image description here

and I want to code $row[1], and it gives me Kraków, $row[2] and it gives me Wolbrom etc.

How would I create it?

>Solution :

You could do something like

$row = array();
while ($temprow = $result->fetch_array(MYSQLI_ASSOC)) {
  $row[$temprow['id']] = $temprow['nazwa'];

Basically just retrieve each row (your current code doesn’t retrieve any data) and put it into the array using the id column as the array index.

Leave a Reply