Ignacio Colautti - 2013-01-01 22:08:40 -
In reply to message 1 from Jay Lepore
Search about PDO::FETCH_ASSOC and modify the funcion ;)
I'm nice... this is my function:
public function query_secure($sql_statement, $params, $fetch_rows=false, $unnamed=false, $delimiter="[[@]]", $assoc = true){
$this->err_msg = "";
if(!isset($unnamed)) $unnamed = false;
if(trim((string)$delimiter)==""){
$this->err_msg = "Error: Delimiter are required.";
return false;
}
if($this->con!=null){
$obj = $this->con->prepare($sql_statement);
if(!$unnamed){
for($i=0;$i<count($params);$i++){
$params_split = explode($delimiter,$params[$i]);
(trim($params_split[2])=="INT") ? $obj->bindParam($params_split[0], $params_split[1], PDO::PARAM_INT) : $obj->bindParam($params_split[0], $params_split[1], PDO::PARAM_STR);
}
$this->sql=$obj->queryString;
try{
$obj->execute();
}catch(PDOException $e){
$this->err_msg = "Error: ". $e->getMessage();
return false;
}
}else{
try{
$obj->execute($params);
$this->sql=$obj->queryString;
}catch(PDOException $e){
$this->err_msg = "Error: ". $e->getMessage();
return false;
}
}
if($fetch_rows==='first')
return $obj->fetch($assoc ? PDO::FETCH_ASSOC : PDO::FETCH_BOTH);
elseif($fetch_rows==='single')
return $obj->fetchColumn();
elseif($fetch_rows)
return $obj->fetchAll($assoc ? PDO::FETCH_ASSOC : PDO::FETCH_BOTH);
if(is_numeric($this->con->lastInsertId()))
return $this->con->lastInsertId();
return true;
}else{
$this->err_msg = "Error: Connection to database lost.";
return false;
}
}