php – Diferencia entre new mysqli y mysqli_connect


I've been reading about it but ca n't figure out when to use one or the other.

I see that the new mysqli is used object-oriented and the mysqli_connect procedure-oriented but it is something that I do not understand.

In this doc: mysqli :: __ construct – mysqli_connect , it doesn't explain much for an amateur.

Could someone instruct me?


There are actually two styles of programming , one is the so-called procedural or procedural style (older, if you will), and the other is the object-oriented style.

Responding to what seems to be your main question, since you have written it in bold:

I can't know when to use one or the other

The answer from the PHP Manual is as follows:

Mix styles

It is possible to switch between the styles at any time. Mixing the two styles is not recommended for clarity and code style reasons .

* PHP manual (see link at the end)

The PHP Manual also explains that the mysqli extension offers a dual interface. It supports the procedural and object-oriented programming paradigm. Giving details about what each one consists of:

The procedural interface

Users migrating from the old mysql extension may prefer the procedural interface. This interface is similar to the old mysql extension. In most cases, function names differ only by prefix. Some mysqli functions take a connection manager as the first argument, while similar functions in the old mysql interface take it as the optional last argument.

The object-oriented interface

In addition to the classic procedural interface, users can choose to use the object-oriented interface. The documentation is organized according to the object-oriented interface. This interface shows functions grouped by purpose, making getting started easier. The reference section provides examples for both syntax variants.

* PHP manual

With regard to doubts raised in various forums and possible myths that may be created about the performance of using one style or another, the Manual says the following:

There are no significant performance differences between the two interfaces . Users can base their choice on their personal preferences.

* PHP manual

A question that will arise, almost certainly …

Having clarified that, I suspect that another question will arise: Which one to decide on?

Indeed, it is not about knowing when to use one or the other (since it is not recommended to mix styles), but about deciding on one or the other. I would recommend, without any fear of being wrong, that you use the object-oriented style.

Among its main advantages we have:

  • Code reuse : for example, you could have a class dedicated to handling your connections to the database and the different methods of querying and presenting the data obtained, etc. You could use that same class in more than one project. In the procedural style, code reuse is not entirely impossible, but it is partial and complicated.

  • Easier to maintain : an advantage that has to do with the previous one. By having your code in one place and reusing it, any application you develop will be easier to maintain. This is not as important in small projects, but in large projects, when you use procedural programming and something needs to be changed or corrected, you will have a big headache. Let's imagine something as simple and frequent as changing the password to access the database. If you have database connection code everywhere in your application, you will have to search one by one to change the password. On the other hand, if you have a connection class that you use every time you need it by creating instances of it using new , you would only have to make the change once, in the class or in the file that it uses to read the password.

  • Modularity : Also useful in large projects. And one of the trends that modern programming is pointing towards. It also facilitates teamwork.

  • Actual programming : (This is a note of mine). Object-oriented programming brings what we program closer to reality. In it you would have Classes that would represent, in any application, real life entities. Thus, a class called Persona would have all the attributes of a person: Nombre, Apellidos, Sexo, Fecha de Nacimiento, etc A Caballo class would have the attributes of a Caballo and would extend from the Animal class. Using OOP programs with reality in mind, which is a huge advantage.

There are other advantages, which I will mention, although they may seem complicated terms if you are just starting out. Surely you will hear about it and you will end up putting it into practice when you advance in the subject: encapsulation, abstraction, inheritance, polymorphism .

This and other advantages offers object-oriented programming.

Another possibility regarding database management

If you decide to use MySQLi, I would recommend using the object-oriented style. Although I in particular, for Database management I prefer the PDO class , because it is more powerful and allows to operate with more simplicity on the data.

(*) For more details and examples of both styles with code, you can consult the section Dual interface: procedural and object-oriented in the PHP Manual, from which I have taken several elements to answer your question.

Scroll to Top