When trying to make the statement to pass it to the database I get the following errors.
User repository code:
public static function insertar_usuario($conexion, $usuario){
$usuario_insertado = false;
if (isset($conexion)){
try{
$sql = "INSERT INTO usuarios(nombre, email, password, fecha_registro, activo)
/*AQUI PONEMOS ALIAS*/VALUES(:nombre, :email, :password, NOW(), 0)";
$sentencia = $conexion -> prepare($sql);
$sentencia -> bindParam(':nombre', $usuario -> obtener_nombre(), PDO::PARAM_STR);
$sentencia -> bindParam(':email', $usuario -> obtener_email(), PDO::PARAM_STR);
$sentencia -> bindParam(':password', $usuario -> obtener_password(), PDO::PARAM_STR);
$sentencia -> execute();
$usuario_insertado = $sentencia -> fetch();
}catch (PDOException $ex){
print 'ERROR'. $ex->getMessage();
}
}
return $usuario_insertado;
}
I do not know how to fix it.
And I also get this following error:
Config code:
define('NOMBRE_SERVIDOR', 'localhost');
define('NOMBRE_USUARIO', 'root');
define('PASSWORD', '');
define('NOMBRE_BD', 'blog');
Connection :
class Conexion{
private static $conexion;
public static function abrir_conexion(){
if(!isset(self::$conexion)){
try{
include "config.inc.php";
self::$conexion = new PDO('mysql:host='.NOMBRE_SERVIDOR.'; dbname='.NOMBRE_BD.'; port=3307; charset=utf8', NOMBRE_USUARIO, PASSWORD);
self::$conexion -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Si ponemos lo siguiente comentado no funciona
//self::$conexion -> exec("SET CHARACTER utf8");
}catch(PDOException $ex){
print "<db>"."ERROR: " . $ex -> getMessage() . "<br>";
die();
}
}
}
public static function cerrar_conexion(){
if (isset(self::$conexion)){
self::$conexion = null;
}
}
// Para usar referencia a conexion fuera de esta clase ya que es privada
public static function obtener_conexion(){
return self::$conexion;
}
}
This one thought that changing it to define instead of variables would fix it, but it doesn't work for me.
This is for PHP in general: when you get the message, only values should be passed by reference , it means that the statement in question cannot internally execute a function or method call.
In other words: you must first assign the return value to a variable and then put it in the statement. Then:
And the second error is because the config file is being imported from another file that is called from
conexión
, that is, multiple times. To solve - quickly - that problem, useinclude_once 'config.inc.php'
orrequire_once 'config.inc.php';