I have the following php DB class with prepared queries:
<?php
class BD {
//conexion con base de datos
$cadena = "mysql:host=127.0.0.1;dbname=idCliente00XX";
$user = "root";
$password = "";
$conn;
$depuracion = true;
function open() {
try {
$this->conn = new PDO($this->cadena, $this->user, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn->exec("set character set utf8");
} catch (PDOException $e) {
if ($this->depuracion)
echo $e->getMessage();
$this->conn = NULL;
die();
}
}
function CerrarConexion() {
$this->conn = NULL;
}
//$this->bd->ConsultaPreparada("SELECT * FROM tabla WHERE correo=? AND password=? AND tipousuario=?", array($correo, $password, $tipo));
function ConsultaPreparada($sql, $parametros) {
if ($this->conn == NULL)
$this->open();
$sentencia = $this->conn->prepare($sql);
//echo $sql;
if ($sentencia->execute($parametros)) {
return $sentencia->fetchAll(PDO::FETCH_ASSOC);
} else {
if ($this->depuracion)
echo var_dump($sentencia->errorInfo());
return null;
}
}
//this->bd->InsertarRegistrosPreparada("INSERT INTO tabla (id,campo2) VALUES (?,?)", array($ide, $camp)))
public function InsertarRegistrosPreparada($sql, $parametros) {
if ($this->conn == NULL)
$this->open();
$sentencia = $this->conn->prepare($sql);
if ($sentencia->execute($parametros)) {
return TRUE;
} else {
if ($this->depuracion)
//var_dump($sentencia->errorInfo());
return FALSE;
}
}
//$this->bd->ModificarRegistrosPreparada("UPDATE tabla SET titulo = ? WHERE id = ? AND estatus <> ?", array($nombre, $id, 2)))
public function ModificarRegistrosPreparada($sql, $parametros) {
if ($this->conn == NULL)
$this->open();
$sentencia = $this->conn->prepare($sql);
if ($sentencia->execute($parametros)) {
return TRUE;
} else {
if ($this->depuracion)
// echo var_dump($sentencia->errorInfo());
return FALSE;
}
}
//$this->EliminarRegistro("DELETE FROM granja WHERE id_granja=?", array($id));
public function EliminarRegistro($sql,$parametros) {
if ($this->conn == NULL)
$this->open();
$sentencia = $this->conn->prepare($sql);
if ($sentencia->execute($parametros)) {
return TRUE;
} else {
if ($this->depuracion)
// echo var_dump($sentencia->errorInfo());
return FALSE;
}
}
function ConsultaAsociativaOrdenada($tabla, $parametros) {
if ($this->conn == NULL)
$this->open();
$sentencia = $this->conn->prepare("SELECT * FROM " . $tabla);
if ($sentencia->execute($parametros)) {
return $sentencia->fetchAll(PDO::FETCH_ASSOC);
} else {
if ($this->depuracion)
echo var_dump($sentencia->errorInfo());
return null;
}
}
function ModificacionAlter($sql, $parametros) {
if ($this->conn == NULL)
$this->open();
$sentencia = $this->conn->prepare($sql);
//echo $sql;
if ($sentencia->execute($parametros)) {
return TRUE;
} else {
if ($this->depuracion)
// echo var_dump($sentencia->errorInfo());
return FALSE;
}
}
}
?>
I access the database without problems as long as the first string variables, user, password have var before being declared. when removing them it no longer allows to connect and sends me the following error
Parse error: syntax error, unexpected '$cadena' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST) in
I was working with php 5.5 and kept the var declaration, now I need to be able to concatenate two strings and store their content in another, which sent me an error in the concatenation variable, they told me that the version of php now handles the declaration obsolete of var, so I assumed that by uploading the php version I could perform the concatenation without problems, but I already uploaded the version and it throws me the error in the first variable.
You have to put the modifier to the members of the class, as the documentation says :
For example, assuming all members are
private
: