Error running mysql_query

Asked

Viewed 120 times

-3

When I execute the mysql_query it gives the following error(even if I manage to connect to the database without major problems)

Warning: mysql_query(): Access denied for user ''@'localhost' (using password: NO) in C:\xampp\htdocs\LuckTor\index.php on line 57

Warning: mysql_query(): A link to the server could not be established in C:\xampp\htdocs\LuckTor\index.php on line 57

The entire code

        $connect = mysqli_connect("localhost", "user", "senha", "bd") or die('ERRO AO LOGAR!');
        if (!$connect) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
        $name = 'Barbie e sua amiga no paraiso';
        $categoria = 'Serie';
        $download = 1569;
        $upload = 1694;
        $magnet = 'magnet:?xt=urn:btih:B250C87526B32E9421AA8FB7599FC11CF3FD1275&dn=Morgan%20-%20A%20Evolu%c3%a7%c3%a3o%202016%20Bluray%20720p%20Dublado%20-%20TPF&tr=udp%3a%2f%2ftracker.openbittorrent.com%3a80%2fannounce&tr=udp%3a%2f%2ftracker.opentrackr.org%3a1337%2fannounce';

        $query = mysql_query("INSERT INTO torrents(`Nome`, `Categoria`, `DownloadQuantity`, `UploadQuantity`, `Magnet`) VALUES ('".$name."', '".$categoria."', '".$download."', '".$upload."', '".$magnet."')");

2 answers

4

You are using mysqli_connect and using mysql_query without the i, it does not find the connection and says access denied to the user '' Because he can’t find the connection.

3


Try to do this way in this connection order: DB_HOST, DB_NOME_USUARIO, DB_SENHA, DB_NOME_BANCO. Behold:

$conn = new mysqli("localhost", "user", "senha", "bd");

// verifica se encontrou algum erro na conexão
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();            
}  

See also that the $download and $upload are as whole, but in query you are using '".$upload."' and '".$download."', as strings.

 $query = mysql_query("INSERT INTO torrents(`Nome`, `Categoria`, `DownloadQuantity`, `UploadQuantity`, `Magnet`) 
VALUES ('".$name."', '".$categoria."', '".$download."', '".$upload."', '".$magnet."')");

Do so:

 $query = mysqli_query($conn, "INSERT INTO torrents(`Nome`, `Categoria`, `DownloadQuantity`, `UploadQuantity`, `Magnet`) VALUES ('".$name."', '".$categoria."', ".$download.", ".$upload.", '".$magnet."')");

In addition to being not finding a connection according to the user.

  • I did it that way and the mistake continues.

  • I tested the connection, and the error remains the same.

  • @Lucascarezia the username and password is the same?

  • Absolutely, since I can connect with the mysqli_connect without returning errors.

  • Returns Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\LuckTor\index.php on line 56

Browser other questions tagged

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