I am having a problem with mysqli. We know mysqli needs two parameter to execute. One is the "query" and other is the "php connection line code". Now, I want to make a separate connection file and want to write include "that separate connection file" in each file so that I do not need to write the connection code in each file or if I change the connection file, all files get the update.
But in that case, I will not have the connection line code in every file so I will have only one parameter to execute mysqli query so I will not be able to execute it. Any suggestions? I ignored code because Stack-overflow.com has too many restrictions on it.
IN simplified psuedo-code:
connection.php:
any other script:
require()
andinclude()
act is if you'd literally cut & paste the included file into the file doing the including. There's no functional difference betweenand
As long as the file exists and can be included/required, it'll be as if you had its contents stuffed directly into the parent file.
Mark B
did answer the question although his answer was for mysql -which is deprecated and shouldn't be usedby the way it is mysqli and not mysquli
Procedural style
let's say the connection file was:
conn.php
and the other file:
other_file.php
OOP style
conn.php
and the other file:
other_file.php
Here I used the normal
mysqli::query
andmysqli_query
(which are the same), but I would also recommend using prepared statements rather thanmysqli::query
because it is safer for SQL injection.