phpmysqli

Call to undefined method mysqli::execute_query()


This are the files I'm 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:\xampp\htdocs\main\Modelo\modeloLogin.php:13 Stack trace: #0 C:\xampp\htdocs\main\Control\controladorLogin.php(12): modeloLogin->existe('1') #1 C:\xampp\htdocs\main\Control\superControlador.php(11): controladorLogin::chequear(Array) #2 {main} thrown in C:\xampp\htdocs\main\Modelo\modeloLogin.php on line 13


Solution

  • 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.