ERROR IN: Call to a member function fetch_assoc () on a non-object in

2

I'm trying to install crea8SOCIALPRO V6.2.1 , but when I install the db redirects me to

  

Call to a member function fetch_assoc () on a non-object in   /public_html/includes/core.php on line 1015.

This is the code that gives me error:

public static function getSitePages() {
        if (cache_exists("site-pages")) {
            return get_cache('site-pages');
        } else {
            $query = db()->query("SELECT * FROM static_pages");
            $pages = array();
            //echo db()->error;
             while($fetch = $query->fetch_assoc()) {
                $pages[$fetch['slug']] = $fetch;
            }
            set_cacheForever("site-pages", $pages);
            return $pages;
        }

Line 1015 would be while($fetch = $query->fetch_assoc()) {

    
asked by Phoenix SG 15.11.2016 в 07:43
source

2 answers

3

Try to put: $result->fetch_array()

The solution would be:

$query = "SELECT * FROM static_pages";
$result = db()->query($query);

$total_num_rows = $result->num_rows;

while($row = $result->fetch_array())
{
    print_r($row);
}
    
answered by 15.11.2016 / 07:51
source
3

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!

    
answered by 31.12.2016 в 16:23