Question

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,

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top