The mysqli_query function is for you to execute SQL statement when it is already connected. For example:
mysqli_query("SELECT * FROM users");
To connect to the database, you must use the mysqli_connect
function or the new mysqli()
constructor. Ex:
Object Orientation
$db = new MySQLi("localhost", "usuário-db", "senha-db", "database", "porta-do-mariadb")
if ($db->connect_error) {
die("Error: " . $db->connect_error);
}
Using Static Method
$db = MySQLi::connect("localhost", "usuário-db", "senha-db", "database", "porta-do-mariadb")
if ($db->connect_error) {
die("Error: " . $db->connect_error);
}
Using procedural programming
$db = mysqli_connect("localhost", "usuário-db", "senha-db", "database", "porta-do-mariadb")
if (!$db) {
die("Error: " . mysqli_connect_error());
}
If the port of your connection is the default (3306), you do not need to put it. It is already set to php.ini
Configuring data through php.ini
If you choose to configure the data using php.ini
, you do not have to pass any parameters. This will force PHP
to use the settings set in the above file.
In this case you can open your php.ini
and add the following lines:
[mysqli]
mysqli.default_host = 127.0.0.1
mysqli.default_port = 3306
mysqli.default_user = "usuário-do-db"
mysqli.default_pw = "senha-do-db"
Using this way you can connect as follows:
/* Orientado a Objeto */
$db = new MySQLi();
$db->select_db("nome-do-db");
/* Procedural */
$db = mysqli_connect();
mysqli_select_db($db, "nome-do-db");
After one of the methods listed above, you can use the mysqli_query >.