简体   繁体   中英

Fatal Error class not found

Good night.

Please excuse me for my english, I'm from Costa Rica...

I am working with PHP POO and I have a problem.

The error is the next:

Fatal error: Class 'mysql' not found in /home/casa/public_html/guaria/clases/verificaSesion.php on line 15

All my .php files are in the "clases" folder.

conexion.php

<?php
/**
* Clase de MySQL
*/
class mysql
{
    private $con;
    private $res;
    private $reg;
    public function conecta()
    {
        $con = mysqli_connect("localhost","root","casa","trabajo1");
        if (!$con) {
            echo "Error al conectar a base de datos".mysqli_error();
        }
        mysqli_query($con, "SET NAMES'utf8'");
        $this->con = $con;
    }
    public function consulta($sql)
    {
        $res = mysqli_query($this->con,$sql);
        $this->res = $res;
        return;
    }
    public function devuelve()
    {
        $reg = mysqli_fetch_assoc($this->res);
        return $reg;
    }
    public function desconecta()
    {
        mysqli_close();
    }
}
require_once 'acciones.php';
?>

And this is my verificaSesion.php file:

<?php
require_once 'conexion.php';

/**
* Clase de Verificación de Sesión
*/
class verificaSesion
{
    public $mysql;
    public $usuario;
    public $password;
    public $direccion;
    function __construct($direccion)
    {
        $this->mysql = new mysql();
        $this->mysql->conecta();
        $this->usuario = $_COOKIE["usuario"];
        $this->password = $_COOKIE["password"];
        $this->direccion = $direccion;
    }
    public function verifica()
    {
        $res = $this->mysql->consulta("select count(*) as total from usuarios where usuario='".$this->usuario."' and password='".$this->password."'");
        $reg = $this->mysql->devuelve();
        $tot = $reg["total"];
        if ($tot == 1) {
            return 1;
        } else {
            return 2;
        }
    }
    public function obtiene()
    {
        $res = $this->mysql->consulta("select * from usuarios where usuario='".$this->usuario."'");
        $reg = $this->mysql->devuelve();
        return $reg;
    }
    public function redirecciona()
    {
        header("Location: ".$this->direccion);
    }
    public function ejecuta()
    {
        if ($this->verifica() == 1) {
            return $this->obtiene();
        } else {
            $this->redirecciona();
        }
    }
}
?>

Both files are in the same folder so I dont understand the problem...

Thank you

The reason for this is because mysql is a reserved keyword for the mysql connection library.

How to Fix It:

Just use a different name

Ok, I finally make my code Work. By the way I can create a class called mysql because mysql function is not a class like mysqli. if it could not create a class called mysql it would show an error like:

Fatal error: Cannot redeclare class mysql

But it doesn't..

The solution was to change the require_once in verificaSesion.php to:

require_once 'clases/conexion.php';

I dont really understand why, because both files verificaSesion and conexion are in the same folder but it works... Anyway, thank you for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM