$data->fetch_assoc(); vs mysqli_fetch_assoc($select)

Been trying to look this up and just want to be sure this is the exact same thing in terms of outcome and resource usage.

Right now I do :

$data = mysqli_fetch_assoc($select)

But I see others doing:

$data = $select->fetch_assoc();

Is this pretty much the exact same thing, just a preference in coding style?

I’m trying to get the hang of mysqli is I move from mysql and still some confusing aspects.

Cheers
Ryan

Are you sure you weren’t looking at procedural vs. OOP style syntax?
http://php.net/manual/en/mysqli-result.fetch-assoc.php

Object oriented style
array mysqli_result::fetch_assoc ( void )

Procedural style
array mysqli_fetch_assoc ( mysqli_result $result )

That is what I was looking at, so it’s just “style” and nothing us. No benefit either way, just preference of coding with same exact function/result.

Correct?

Cheers
Ryan

I saw a post by @felgall recently where he said procedural and OOP can be mixed, but I’ve never seen any code that way and I haven’t tried mixing the two.

So yes, it is more of a preference, some like OOP and are comfortable using that style, others like and are more comfortable using procedural style.

I guess a benefit with using OOP style could be that you could write code extending the class, but if you’re only using it I don’t think it makes much difference between resource vs. object

Phew! I’m more of a procedural guy so would have been bummed to hear the OOP is awesomely more efficient. lol

You’re not alone. It still amazes me how many members here cling to using DEPRECATED mysql_ because seeing OOP and mysqli_ together in the same context bothers them.

mysqli_ procedural is the logical step to take if OOP isn’t an option.
At times I feel like posting an ALL CAPS reply
“For goodness sake, write mysql_ out of your code soon especially if you’re rewriting the code anyway!”

@casbboy @Mittineague
The only one at the moment that sticks out to me is using prepared statements. I recently (a few months back helping someone with prepared statements), found out that you cannot use procedural prepared statements with OOP connection. What amazes me the most is there’s such a thing for procedural in the first place. I thought prepared statements was strictly for OOP style hence prepared statements, but I was wrong. http://php.net/manual/en/mysqli.prepare.php

I’m not sure exactly why you can’t do this if you can mix match procedural and OOP together, but I’m assuming because using prepared statements are specific to which connection they are opened with.

I could be wrong.

That makes sense since the SQL you prepare has to be processed ready to be executed )possibly multiple times) at a later time.

When I switched from mysql_ to mysqli_ calls I set up the connection OOP style and just converted the select calls to use procedural style queries to start with. The insert, update and delete calls all got updated to OOP prepare statements. Next step is to convert the select statements to use OOp prepare statements (if I don’t decide to swap to PDO first).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.