PHP Classes

Extra rows in return array

Recommend this page to a friend!

      PDO Multi Connection Class  >  All threads  >  Extra rows in return array  >  (Un) Subscribe thread alerts  
Subject:Extra rows in return array
Summary:Index number rows being returned in query array
Messages:2
Author:Jay Lepore
Date:2013-01-01 21:58:18
Update:2013-01-01 22:08:40
 

  1. Extra rows in return array   Reply   Report abuse  
Picture of Jay Lepore Jay Lepore - 2013-01-01 21:58:18
When I run a query (secure or not) my return array has a number index inserted between each key/value pair.

## Here is example query:
$params = array(":site_id|ws-000000000-ci|varchar");
$rows = $db->query_secure("SELECT * FROM esites_control WHERE site_id=:site_id;", $params, true, false);

## Here is example foreach to extract values
foreach($rows as $key=>$value){
foreach($value as $kkey=>$vvalue){
${$kkey}=$vvalue;
echo $kkey." = ".$vvalue."<br>";
}
}

## Here is result of for each loop
site_model =
0 =
site_number = 0
1 = 0
site_id = ws-000000000-ci
2 = ws-000000000-ci
site_url = sta.compumatter.com
3 = sta.compumatter.com
base_url = compumatter.com
4 = compumatter.com
site_scope_id = 1
5 = 1

/* You will notice that there is an index 0,1,2,3 appearing below each key value pair and this index is assigned an identical value of its previous row.

Why is that ? Is this a bug to remove. I am having to use the code
if(!is_int($kkey)){
echo $kkey." = ".$vvalue."<br>";
}
to detect if key is an 'int' and not reflect this false reading.

Your thoughts ?

Jay Lepore
CompuMatter
*/

  2. Re: Extra rows in return array   Reply   Report abuse  
Picture of Ignacio Colautti 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;
}
}