relatively newbie with a PHP5/MySQLi array issue

liunx

Guest
Hi there!

I'm relatively new to php5 and mysqli (altho I have been using php4 for a while, still pretty much a n00b tho! LOL!)

Now I have written this 'ere bit of code:


if ($stmt = $mysqli->prepare("SELECT item, name, template FROM ".TABLE_MENU." ORDER BY Name")) {
$stmt->execute();

/* bind variables to prepared statement */
$stmt->bind_result($item, $name, $template);

/* fetch values */
while ($stmt->fetch()) {
$tmp = array("item" => $item,
"name" => $name,
"template"=> $template);

$menu[$item] = $tmp;
}

$stmt->close();
}

$mysqli->close();

return $menu;


Which returns this array:


Array (4)
aboutus => Array (3)
item => aboutus
name => About Us
template => about
gigs => Array (3)
item => gigs
name => Gigs
template => gigs
home => Array (3)
item => home
name => Home
template => home
media => Array (3)
item => media
name => Media
template => media


The thing is... I have this nagging feeling that I have done it in a rather cack handed way, altho the output is exactly what i want!

Would anyone care to give me any insights or words of advice?

Many thanks,

d.That looks reasonable, although instead of this:while ($stmt->fetch()) {
$tmp = array("item" => $item,
"name" => $name,
"template" => $template);

$menu[$item] = $tmp;
}I'd do this:while ($stmt->fetch()) {
$menu[$item] = array("name" => $name,
"template" => $template);
}Thanks for the reply, I'll give that a go, slim it down a bit. :)I'm not in a position to try this (and I haven't tried it in the past, never having used the MySQLi extension), but can you bind to an array element?
$stmt->bind_result($row['item'], $row['name'], $row['template']);
And then
while ($stmt->fetch()) {
$menu[$row['item']] = $row;
} You've still got $item as both the key and part of the value, but I don't see that hurting.
 
Back
Top