Mysqli_error () expects parameter 1 to be mysqli, null given

0

I'm trying to show some data in my browser from the database and when I run it it shows me the following error:

mysqli_error () expects parameter 1 to be mysqli, null given

I would appreciate some help.

Code:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
</head>
<body>

<table border='1px'>
    <tr>
    <td class='fila'>id</td>
    <td class='fila'>Nombre</td>
    <td class='fila'>Apellido</td>
    </tr>
<?php


$host="localhost";
$usuario="php";
$password="prueba";
$basedatos="clientes";

$connection = mysqli_connect($host,$usuario,$password);

function showerror( )   {
    die("Se ha producido el siguiente error: " . mysqli_error($connection));
}
    // Seleccionar la base de datos
if (!(@ mysqli_select_db($basedatos, $connection)))
showerror( );

$SQL=" SELECT id_cliente,nombre,apellido FROM clientes.datos where 
fecha_creacion between '2018-06-15' and '2018-06-01' ";

    if (!($result1 = @ mysqli_query ( $SQL, $connection)))
    showerror( );

while ($row1 = mysqli_fetch_row($result1)) {



    print("<tr>");
    print("<td class='fila'>".$row1[0]."</td>");
    print("<td class='fila'>".$row1[1]."</td>");
    print("<td class='fila'>".$row1[2]."</td>");
    print("</tr>");


}


?>



</body>
</html>
    
asked by hayber 15.06.2018 в 16:10
source

1 answer

1

Probably because of your connection string. Test placing:

$connection = mysqli_connect($host,$usuario,$password, $basedatos);


if (mysqli_connect_errno()) {
    printf("Falló la conexión: %s\n", mysqli_connect_error());
    exit();
}

function showerror( )   {
    die("Se ha producido el siguiente error: " . mysqli_error($connection));
}

By including the bd in your connection string, this segment of code is no longer necessary:

// Seleccionar la base de datos
if (!(@ mysqli_select_db($basedatos, $connection)))
showerror( );

At the end your debugged code would be the following:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
</head>
<body>

<table border='1px'>
    <tr>
    <td class='fila'>id</td>
    <td class='fila'>Nombre</td>
    <td class='fila'>Apellido</td>
    </tr>
<?php


$host="localhost";
$usuario="php";
$password="prueba";
$basedatos="clientes";

$connection = mysqli_connect($host,$usuario,$password, $basedatos);

if (mysqli_connect_errno()) {
    printf("Falló la conexión: %s\n", mysqli_connect_error());
    exit();
}


function showerror( )   {
    die("Se ha producido el siguiente error: " . mysqli_error($connection));
}


$SQL=" SELECT id_cliente,nombre,apellido FROM clientes.datos where 
fecha_creacion between '2018-06-15' and '2018-06-01' ";

    if ($result1 = mysqli_query($connection, $SQL))
    {

        while ($row1 = mysqli_fetch_row($result1)) {

            print("<tr>");
            print("<td class='fila'>".$row1[0]."</td>");
            print("<td class='fila'>".$row1[1]."</td>");
            print("<td class='fila'>".$row1[2]."</td>");
            print("</tr>");

        }
    }

?>

</body>
</html>

For more information you can review mysql_query

I hope it's helpful, greetings!

    
answered by 15.06.2018 / 16:44
source