-1

I trying to run my query but everytime I try to run it, it would give me this kind of error: Call to a member function fetchAll() on a non-object.

this is my code:

Single.php:

<?php
require 'admin/config.php';
require 'functions.php';

$conexion = conexion($db_config);
$url_canal = url_canal($_GET['url']);

if (!$conexion) {
    header('Location: error.php');
}

if (empty($url_canal)) {
    header('Location: index.php');
}

$canal = obtener_canal_por_url($url_canal, $conexion);

if(!$canal) {
    header('Location: index.php');
}

$canal = $canal[0];

require 'views/single.view.php';

?>

functions.php:

function limpiarDatos($datos) {
    $datos = trim($datos);
    $datos = stripslashes($datos);
    $datos = htmlspecialchars($datos);
    return $datos;
}
function url_canal($url) {
    return limpiarDatos($url);
}

function obtener_canal_por_url($url, $conexion) {
    $resultado = $conexion->query("SELECT * FROM canales WHERE url = $url LIMIT 1");
    $resultado = $resultado->fetchAll();
    return ($resultado) ? $resultado : false;
}

Can you help me to solve this, please? i want to know where is my mistake. Thank you

Conexion:

function conexion($db_config) {
try {
    $conexion = new PDO('mysql:host=localhost;dbname='. $db_config['bd'], $db_config['usuario'], $db_config['pass']);
    return $conexion;
} catch (PDOException $e) {
    return false;
}

}

Erick ls
  • 186
  • 8

2 Answers2

1

replace your query with

$resultado = $conexion->query("SELECT * FROM canales WHERE url = '$url' LIMIT 1");
shubham715
  • 3,324
  • 1
  • 17
  • 27
0

Check your query (I guess url is a string):

$resultado = $conexion->query("SELECT * FROM canales WHERE url = '" . $url . "' LIMIT 1");

Then check $resultado before fetchAll():

if ($resultado) {
    $rows = $resultado->fetchAll();
}
devneri
  • 81
  • 3