mariadb connection error

Asked

Viewed 1,283 times

1

Today I installed the lamp in my manjaro and everything was working normally, until I went to test php with mysql and typed the code:

$conexao = mysqli_connect('localhost','root','','data');

I realize that after that, all the rest of the code is discarded, as if the code ended there, even the html code typed after that is discarded, I don’t know what to do

1 answer

1

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

  • @Gabriellothric Try to do as I mentioned in "Using procedural programming"

  • Add the condition and die to check the error.

  • I tried every method, none worked, and the die does not appear

  • @Gabriellothric edits your question and adds how you’re running the queries.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.