Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to create array from MYSQL answer in PHP?

i have code like that:

require_once("../db.php");
mysqli_report(MYSQLI_REPORT_STRICT);

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 {
     //note
    }
  }
}
catch (Exception $e) {
echo "Server error";
}

my MYSQL table looks like this:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading