Please see the php function below. I am trying to get the results of my query to display as an array in the same format as the static array shown below. How would I replace that static array with results from my query? Thanks in advance.
<?php
$mysqli = new mysqli("xxx", "xxx", "xxx", "xxx");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s
", $mysqli->connect_error);
exit();
}
$query = "SELECT event_id, event_name FROM events ORDER by event_id";
$result = $mysqli->query($query);
$row = $result->fetch_array(MYSQLI_BOTH);
function get_price($find){
$books=array(
"java"=>266,
"c"=>250,
"php"=>320
);
foreach($books as $book=>$price)
{
if($book==$find)
{
return $price;
break;
}
}
}
?>
As you stated: Normally you would create an array then fill it with all the items found in the database (or whatever source you're going to fetch it from).
Side note: Instructing the MySQLi extension to fetch both associative and numeric is a bit time-wasting (and consuming, because the processor has to spend cycles on both).
It would look something like this:
$array = [];
while ($row = $result->fetch_object()) { // fetch as object, nicer ;)
$array[$result->title] = $result->price;
}
The array would now look something like:
$array = [
'Jungle book' => 26.95,
'Mowgli' => 24.75,
'Fantastic Four - E.12-6' => 100
];