I got this php code at the end of my php file, but I got and error that says:
Notice: Undefined variable: array_multi in C:\wamp\www\getPets.php on line 52
$j = 1;
while ($row=mysqli_fetch_assoc($result))
{
foreach ($row as $colname => $value)
{
$array_multi[$j][$colname] = $value;
}
$j++;
}
return $array_multi;
}
?>
But I do not know why I am getting this error..
Thanks!!
PHP questions.
Moderator: General Moderators
Re: PHP questions.
Have you declared $array_multi before using it?
It will automatically try declaring the variable if it doesn't exist from before but the notice is just as a warning incase you e.g misspelt a variable name.
Code: Select all
<?php
//Causes notice
echo $test;
//Does not cause a notice
$think = "hello";
echo $think;
?>Re: PHP questions.
what SvanteH telling is correct. you need to declare following line just before the while loop
$array_multi = array();
$array_multi = array();
Re: PHP questions.
In fact, it's the $array_multi[$j] which also should be initialized:
PS:
In PHP you can use the
which is the same as
or
this way variable $j in some cases is made redundant.
PPS: $row is an associative array, so you can grab the whole of it and add it to the results array:
Code: Select all
$array_multi[$j] = Array();
foreach ($row as $colname => $value)
{
$array_multi[$j][$colname] = $value;In PHP you can use the
Code: Select all
$array[] = $value;Code: Select all
$array[count($array)] = $value;Code: Select all
array_push($array, $value);PPS: $row is an associative array, so you can grab the whole of it and add it to the results array:
Code: Select all
while ($row = mysqli_fetch_assoc($result))
$array_multi[] = $row;
return $array_multi;There are 10 types of people in this world, those who understand binary and those who don't