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
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
andget_result
in turn.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
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 classmodeloLogin
should be made like this: