php error with mysql and mysqli

Asked

Viewed 827 times

0

I’m kind of new to programming and I’m having a hard time, I was programming a site and running it on wampserver, but from a while I updated the wamp version and the php MYSQL commands stopped working, works only when I switch the commands to MYSQLI and even then it’s still giving me the same errors mysqli_query() expects at least 2 parameters, 1 given in ./.../..... or mysqli_error() expects exactly 1 parameter, 0 given in in every select you used, for example:

$variavel = mysqli_query("SELECT * FROM tabela ORDER BY RAND() LIMIT 5") or die(mysqli_error());

the connection part with the bank is so:

    header( 'Content-Type: text/html; charset=utf-8' );
    $servidor = "...";
    $usuarioServidor = "...";
    $senhaServidor = "...";
    $nomeBD = "...";

    $conectar = @mysqli_connect($servidor,$usuarioServidor,$senhaServidor) or die ("Não foi possível acessar o servidor");
    $selesionarBD = @mysqli_select_db($nomeBD,$conectar) or die (mysqli_error());

remembering that when I put MYSQL does not work, only with MYSQLI

Why MYSQL commands are no longer working and why MYSQLI are giving error?

2 answers

3

When you use the function mysqli_query, you must pass two attributes: O Resource and the query.

if (!mysqli_query($conectar, "SELECT * FROM tabela")) {
    die(mysqli_error($conectar));
}

Already in office mysqli_error, you must pass only the attribute Resource.

This attribute allows the function to identify which connection is, which database is selected, etc.

When you work with Object Orientation, this is not necessary.

  • Well, answered before then deleted my answer +1, mysql is obsolete from version 5.5.0 php use mysqli. Mysqli documentation page : https://secure.php.net/manual/en/book.mysqli.php

0

Alessandro Pardo, you must follow the php documentation according to the extension used Improved Mysql extension

The consultation part should look like this:

$variavel = mysqli_query($conectar, "SELECT * FROM tabela ORDER BY RAND() LIMIT 5") or die(mysqli_error($conectar));

The code part of the database should look like this:

header( 'Content-Type: text/html; charset=utf-8' );
$servidor = "...";
$usuarioServidor = "...";
$senhaServidor = "...";
$nomeBD = "...";

$conectar = @mysqli_connect($servidor,$usuarioServidor,$senhaServidor,$nomeBD) or die ("Não foi possível acessar o servidor");
$selesionarBD = @mysqli_select_db($conectar, $nomeBD) or die (mysqli_error($conectar));

Browser other questions tagged

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