The function mysqli_query serves for you to execute SQL command when it is already connected. For example:
mysqli_query("SELECT * FROM users");
To connect to the database it is necessary to use the function mysqli_connect
or the builder new mysqli()
. 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), do not need to put. It is already set in the php.ini
Configuring the data through the php.ini
If you choose to configure the data through php.ini
, it is not necessary to pass any parameters. This will force the PHP
to use the settings set in the above file.
In that 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 make the connection 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 function mysqli_query.
Aaah I was wrong to ask the question, had used mysqli_connect even
– Gabriel Lothric
@Gabriellothric Try to do as I mentioned in "Using procedural programming"
– Valdeir Psr
Add the condition and die to check the error.
– Valdeir Psr
I tried every method, none worked, and the die does not appear
– Gabriel Lothric
@Gabriellothric edits your question and adds how you’re running the
queries
.– Valdeir Psr