skip to Main Content

This are the files im using:

modeloBd.php

<?php
class modeloBd
{
    private static $server = "127.0.0.1";
    private static $user = "root";
    private static $password = "";
    private static $dbname = "bd";

    protected $conn;

    protected function __construct()
    {
        $conexion = new mysqli(self::$server, self::$user, self::$password, self::$dbname);
        if ($conexion->connect_error) {
            die("Error en la conexión a la base de datos: " . $conexion->connect_error);
        } else {
            $this->conn = $conexion;
        }
    }
}

modeloLogin.php

<?php
require_once "modeloBd.php";

class modeloLogin extends modeloBd
{
    public function __construct()
    {
        parent::__construct();
    }

    public function existe($ci)
    {
        $query = "SELECT * FROM `Usuarios` WHERE ci = ? LIMIT 1";
        $result = $this->conn->execute_query($query, [$ci]);
        $num = mysqli_num_rows($result);
        return $num;
    }

controladorLogin.php

<?php
require '../Modelo/modeloLogin.php';
require '../Modelo/modeloToken.php';

class controladorLogin
{
    public static function chequear($content)
    {
        $ci = $content['post']['ci'];
        $contrasenia = $content['post']['contrasenia'];
        $l = new modeloLogin();
        if ($l->existe($ci)) {
        //code
        }
    }
}

I just needed to make my query work in here:

public function existe($ci)
    {
        $query = "SELECT * FROM `Usuarios` WHERE ci = ? LIMIT 1";
        **$result = $this->conn->execute_query($query, [$ci]);**
        $num = mysqli_num_rows($result);
        return $num;
    }

But instead the system gives me this error:

Fatal error: Uncaught Error: Call to undefined method mysqli::execute_query() in C:xampphtdocsmainModelomodeloLogin.php:13 Stack trace: #0 C:xampphtdocsmainControlcontroladorLogin.php(12): modeloLogin->existe(‘1’) #1 C:xampphtdocsmainControlsuperControlador.php(11): controladorLogin::chequear(Array) #2 {main} thrown in C:xampphtdocsmainModelomodeloLogin.php on line 13

2

Answers


  1. The documentation for execute_query says "(PHP 8 >= 8.2.0)".

    It is a new feature in PHP 8.2 which, at the time of writing is the newest version of PHP.

    To use that function you need to upgrade your PHP install. (Note that it is easy to have different versions of PHP installed for command line and web server use, so make sure that you upgrade the version associated with your web server).


    Alternatively you could use the older approach (detailed in the blog post linked above) and call prepare, bind_param, execute and get_result in turn.

    Login or Signup to reply.
  2. This inconvenience can be rather easily mitigated by checking the current PHP version and extending the mysqli class if needed.

    You can add a code like this to where your database connection is created

    $mysqli_class_name = 'mysqli';
    if (version_compare(PHP_VERSION, '8.2', '<')) {
    
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        function mysqli_execute_query(mysqli $mysqli, string $query, ?array $params = null)
        {
            $stmt = $mysqli->prepare($query);
            if ($params) {
                $types = str_repeat("s", count($params));
                $stmt->bind_param($types, ...$params);
            }
            $stmt->execute();
            return $stmt->get_result();
        }    
        
        class my_mysqli extends mysqli {
            public function execute_query(string $query, ?array $params = null)
            {
                return mysqli_execute_query($this, $query, $params);
            }
        }
        $mysqli_class_name = 'my_mysqli';
    }
    $mysqli = new $mysqli_class_name($hostname, $username, $password, $dbname);
    

    This code would universally work for any PHP version >= 7.0. In case current PHP version is below 8.2, it will use the extended version of mysqli class, and original class otherwise.


    Please note that you should never create a database connection in your models. This way your code will create hundreds of mysql connections and kill your database server. The database connection should be passed as a constructor parameter instead.

    Therefore, modeloBd in its current form must be scrapped, while class modeloLogin should be made like this:

    class modeloLogin
    {
        protected $conn;
        public function __construct(mysqli $mysqli)
        {
            $this->$conn = $mysqli;
        }
    
        public function existe($ci)
        {
            $query = "SELECT * FROM `Usuarios` WHERE ci = ? LIMIT 1";
            $result = $this->conn->execute_query($query, [$ci]);
            $num = mysqli_num_rows($result);
            return $num;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search