Thanks to a poster here at SO, I started using MySQLI and prepared statements. However, there is one issue I’m not sure how to adapt from the way I used MySQL.
Before, I would open a connection to MySQL at the beginning of my main PHP script. Then any sub-script or class I used would automatically use the same connection just by calling the mysql_query() call.
Now, with MySQLI, it seems that if I want to use the connection in a class, I have to either pass the connection through the constructor or create a new MySQLI connection.
Is there a way to use a MySQLI connection the same way I was using the MySQL connection before?
take care,
lee
Passing the connection into the constructor is often the preferable method. That is called “Dependency Injection” and is useful for software testing.
However, if you would prefer not to use that method, you can simply access the connection globally inside any class method:
Better though, is to store
$connin a class property:If you are using MySQLi in a procedural context rather than object-oriented, you will need to supply the link parameter. There really isn’t a way around it.