我有以下带有准备好的查询的 php DB 类:
<?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;
}
}
}
?>
只要第一个字符串变量、用户、密码在声明之前有 var,我就可以毫无问题地访问数据库。删除它们时,它不再允许连接并向我发送以下错误
Parse error: syntax error, unexpected '$cadena' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST) in
我正在使用 php 5.5 并保留 var 声明,现在我需要能够连接两个字符串并将它们的内容存储在另一个中,这在连接变量中给我发送了一个错误,他们告诉我 php 的版本现在可以处理声明过时的 var,所以我假设通过上传 php 版本我可以毫无问题地执行连接,但我已经上传了版本,它在第一个变量中抛出了错误。
正如文档所述,您必须将修饰符放在类的成员中:
例如,假设所有成员都是
private
: