Warning: mysqli :: query (): Could not fetch mysqli

2

Show me this error:

  

"Warning: mysqli :: query (): Could not fetch mysqli in   C: \ xampp \ htdocs \ prac_01 \ html \ php \ table.php on line 28

     

Fatal error: Uncaught Error: Call to a member function fetch_assoc ()   on null in C: \ xampp \ htdocs \ prac_01 \ html \ php \ table.php: 29 Stack trace:   0 {main} thrown in C: \ xampp \ htdocs \ prac_01 \ html \ php \ table.php on line 29 "

This is my code:

    $query="SELECT * FROM table_3";
    $resultado= $conexion->query($query);
    while($row= $resultado->fetch_assoc()){

Thanks in advance.

Edit (connection code):

<?php
$usuario = "root";
$password = "";
$servidor = "localhost";
$basededatos = "test_02";
$mysqli = new mysqli("localhost", "root", "", "test_02");
$conexion = mysqli_connect( $servidor, $usuario, "" ) or die ("Error: No se ha podido conectar al servidor.");
$db = mysqli_select_db( $conexion, $basededatos ) or die ( "Error: No se ha podido conectar a la base de datos." );
mysqli_close( $conexion );
?>
    
asked by Breath 19.11.2018 в 13:21
source

2 answers

2

You are really creating two connections.

One here:

$mysqli = new mysqli("localhost", "root", "", "test_02");

And another here:

$conexion = mysqli_connect( $servidor, $usuario, "" ) or die ("Error: No se ha podido conectar al servidor.");
$db = mysqli_select_db( $conexion, $basededatos ) or die ( "Error: No se ha podido conectar a la base de datos." );

Then, you are closing one of the connections that you create and that is precisely the one you use on the other side:

mysqli_close( $conexion );

I would recommend that

You use a single connection, giving preference to the object-oriented style 1 . And that you use the variables, if you have declared them is to use them:

<?php
$usuario = "root";
$password = "";
$servidor = "localhost";
$basededatos = "test_02";
$mysqli = new mysqli($servidor, $usuario, $password, $basededatos);
/*Esto es importante para evitar problemas con acentos*/
$mysqli->set_charset("utf8");
?>

Since we have left the name of the connection as $mysqli , then in the code you will use that name.

$query="SELECT * FROM table_3";
$resultado= $mysqli->query($query);  //aquí usamos ahora $mysqli
while($row= $resultado->fetch_assoc()){
    //manejar resultados
}
/*Es aquí donde hay que cerrar la conexión*/
$mysqli->close();

Notes:

1 In many ways PHP supports two programming styles: procedural and object-oriented , such is the case of mysqli . Mixing styles is not recommended. Although the code works, it becomes difficult to maintain and understand. The object-oriented style is more modern, more intuitive and more elegant than the procedural style, requires less code (many functions of the procedural style are horribly extensive). Although you are free to choose one or the other style, I would always recommend using the object-oriented style. For more details on this you can check this answer .

    
answered by 19.11.2018 / 13:45
source
1

It is possible that you are closing the connection before executing the query. Preferably make sure to add it to the end of your code.

It also seems (according to what I read) that you have procedural code and mixed object-oriented code. Check the examples in the PHP manual: link that might give you a little more light on what are you trying to do

    
answered by 19.11.2018 в 13:35