Tengo la clase Database que tiene 2 metodos
public function select($query = "" , $params = [])
{
try {
$stmt = $this->executeStatement( $query , $params );
$result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $result;
} catch(Exception $e) {
throw New Exception( $e->getMessage() );
}
return false;
}
private function executeStatement($query = "" , $params = [])
{
try {
$stmt = $this->connection->prepare( $query );
if($stmt === false) {
throw New Exception("Unable to do prepared statement: " . $query);
}
if( $params ) {
$stmt->bind_param($params[0], $params[1]);
}
$stmt->execute();
return $stmt;
} catch(Exception $e) {
throw New Exception( $e->getMessage() );
}
}
Y en mi modelo tengo
public function signIn($userName,$password){
return $this->select("select count(*) as o_user_exists from users where username = ? and password = ? ", ["i",$userName,$password]);
}
Cuando envio 1 solo parametro funciona correctamente, no entiendo bien al sintaxys para poder enviarle N parametros
estuve leyendo este post https://www.php.net/manual/es/mysqli-stmt.bind-param.php pero soy abstante novato por eso no se como es la sintaxys en visual studio code no me ayuda con la sintaxys,
Ahora intente con
public function signIn($userName,$password){
return $this->select("select count(*) as o_user_exists from users where username = ? and password = ?", ["ss", $userName,$password]);
}
y en database
public function select($query = "" , $params = [])
{
try {
$stmt = $this->executeStatement( $query , $params );
$result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $result;
} catch(Exception $e) {
throw New Exception( $e->getMessage() );
}
return false;
}
private function executeStatement($query = "" , $params = [])
{
try {
$stmt = $this->connection->prepare( $query );
if($stmt === false) {
throw New Exception("Unable to do prepared statement: " . $query);
}
if( $params ) {
$values = array_values($params[1]);
$stmt->bind_param($params[0], ...$values);
}
$stmt->execute();
return $stmt;
} catch(Exception $e) {
throw New Exception( $e->getMessage() );
}
}
Warning: array_values() expects parameter 1 to be array, string given in /Applications/XAMPP/xamppfiles/htdocs/Model/Database.php on line 43
Warning: Only arrays and Traversables can be unpacked in /Applications/XAMPP/xamppfiles/htdocs/Model/Database.php on line 44
Warning: Wrong parameter count for mysqli_stmt::bind_param() in /Applications/XAMPP/xamppfiles/htdocs/Model/Database.php on line 44
{"error":"Call to a member function fetch_all() on boolSomething went wrong! Please contact support."}
las lineas que marca son son estas dos
43 $values = array_values($params[1]);
44 $stmt->bind_param($params[0], ...$values);
array_values
con un string. Ni puedes intentar desempaquetar un string. Si quieres emular la función que te sugerí, puedes pasar por un lado los valores en un array y en otro array los tipos de dato (en caso de que no todos sean del mismo tipo). No sé si me explico, en la otra respuesta los tipos se obtienen aquí:$types = str_repeat('s', count($values));
porque son todos del tipos
se hace que se repitan tantos tipos (s
) como elementos en$values
. Si tienes tiposs
y tiposi
combinados, puedes pasar una cadena a mano sin más y por otro lado un array con los datos.$types="siss";
sería una cadena con los tipos (aquí imaginamos que el segundo dato es de tipo entero). Por otro lado, un array con los datos:$values=['A',1,'B','C'];
Así, podrás hacer esto para vincular los datos:$stmt->bind_param($types, ...$values);