Always look carefully for errors that you throw PHP
or any other language.
Call to a member function fetch_assoc () on a non-object in /public_html/includes/core.php on line 1015.
In this case the error is telling you that you are trying to execute the function fetch_assoc()
from a variable
that is not an object and therefore fails.
This call is in $row = $result->fetch_array()
, where you are passing your $result
sentence, where your failure should be.
MySQLi object-oriented style
I leave an example how it could be:
PHP Connection:
//Variables conexión.
$servidor = "localhost";
$usuario = "root";
$contrasena = "Tu_contraseña";
$bd = "mi_base_de_datos";
// Conexión
$conexion = new mysqli($servidor, $usuario, $contrasena, $bd);
// Comprobamos conxexión
if ($conexion->connect_error) {
exit("Fallo al conectar a MySQL: " . $conexion->connect_error);
}
Example sentence:
$sentencia = $conexion->query("SELECT * FROM tu_tabla");
//Comprobar algun error al consultar nuestra consulta.
if(!$sentencia) {
//Mensaje de error
printf("Error consulta: %s\n" . $conexion->error);
}
//Total registros.
$total_num_rows = $sentencia->num_rows;
//Comprobamos existencia de registros.
if ($total_num_rows > 0) {
//Recuperamos una fila de resultados como un array asociativo.
while ($row = $sentencia->fetch_assoc()) {
//Ya podemos trabajos con nuestros datos.
echo $row['nombre_columna_a_mostrar'];
#etc.
}
} else {
echo "0 resultados encontrados";
}
//Cerramos conexión.
$conexion->close();
Note : my advice is to use sentences prepare()
or PDO
, let's see some reasons because they are so requested today one day the sentences prepared.
The main and most essential benefit of the prepared statements is the elimination of all the dangers of the manual format:
-
Ready statement makes full formatting. All without the
intervention of the programmer!
-
Prepared statement that makes the proper format (as long as
we are joining our data using the appropriate type).
-
Prepared statements make the format invulnerable.
-
The prepared statement is formatted in the only appropriate place -
just before the execution of the query.
This is why the manual format is so despised today and prepared statements are so honorable.
Manual prepare ():
Greetings!