views:

208

answers:

2

Hey everyone,

I was wondering, if I have some code such as:

$result = $db->query($sql); // dont worry, its escaped
$myData = (array)$result->fetch_assoc();

where

$result->fetch_assoc();

returns a mysqli result object.

Is casting it like this right away good practice? I would imagine this is an expensive call - is this true?

It sure makes things convenient to use immediately after I query for it, but I want to make sure I'm not doing something in bad form.

Thanks for any input,

+7  A: 

According to the mysqli documentation it returns an array anyway...

Greg
Thanks - I really can't figure out why I didn't catch that before :)
barfoon
A: 

If there is no more result - it will return NULL. In this case (array) null can have some sense.

Really it is better check special cases in separated place instead of type casting

kingoleg