PHP - Mysqli correct way

2

I'm starting to use mysqli in my projects, however I've seen two ways to make a connection:

One of them is simple and equal to mysql_connect $db = mysqli_connect(...); I saw in w3schools.

Another is $db = new mysqli('localhost', 'user', 'pass', 'demo');

Is there a difference between the 2? Which is the best?

    
asked by anonymous 25.06.2016 / 19:19

1 answer

6

There's no better way, in the end it's all the same.

Looking at the PHP manual:

  

link

You see that the mysqli_connect function is synonymous with the __construct() method (which is what happens when you use new ), so

$db = mysqli_connect('localhost', 'user', 'pass', 'demo');

is the very same thing that

$db = new mysqli('localhost', 'user', 'pass', 'demo');


Use what "match" more with your code style. As is PHP, which is nothing more than a "script processor" that does not maintain the internal state between calls, other than compiled languages where the modules or even the whole application runs in the same context, it is possible to mix the paradigms without any problem, without advantages and disadvantages, and so it becomes a matter of "aesthetics" and really you choose what suits your workflow.

    
25.06.2016 / 19:37