Hello,
I have started to learn and understand PDO for retriving data from Databases. I was recommended to use this wrapper. https://code.google.com/p/php-pdo-wrapper-class/
The PDO wrapper is pulling the data fine and giving it to me as an ASSOCIATIVE array (I think)
Here is the array when I use print_r
Array
(
[0] => Array
(
[title] => Sitename
[description] => The Mad Hatter
)
[1] => Array
(
[title] => Sitehome
[description] => The Milkyard
)
[2] => Array
(
[title] => Siteuser
[description] => Admin
)
[3] => Array
(
[title] => Sitetag
[description] => Flying Pigs
)
[4] => Array
(
[title] => Sitetheme
[description] => cz01
)
)
Now I am trying to put this output into an array called config so I can use throughout my site. eg
Config['Sitename'] = The Mad Hatter
Config['Sitehome'] = The Milkyard
Config['Siteuser'] = Admin
Config['Sitetag'] = Flying Pigs
Config['Sitetheme'] = cz01
The function that returns the data in the wrapper is the following and it returns it into $Results
public function run($sql, $bind = "") {
$this->sql = trim($sql);
$this->bind = $this->cleanup($bind);
$this->error = "";
echo "SQL: " . $sql . "<br>";
try {
$pdostmt = $this->prepare($this->sql);
if ($pdostmt->execute($this->bind) !== false) {
if (preg_match("/^(" . implode("|", array("select", "describe", "pragma")) . ") /i", $this->sql)) {
return $pdostmt->fetchAll(PDO::FETCH_ASSOC);
} elseif (preg_match("/^(" . implode("|", array("delete", "insert", "update")) . ") /i", $this->sql)) {
return $pdostmt->rowCount();
}
}
} catch (PDOException $e) {
$this->error = $e->getMessage();
return false;
}
}
public function select($table, $where = "", $bind = "", $fields = "*") {
$sql = "SELECT " . $fields . " FROM " . $table;
if (!empty($where)) {
$sql .= " WHERE " . $where;
}
$sql .= ";";
echo "SQL: " . $sql . "<br>";
return $this->run($sql, $bind);
}
And I call the class wrapper as
$Database = new czDatabase();
$results = $Database->select("config", "", "", "title,description");
I have read many many threads about PDO and arrays and all the examples just show you how to query and then to see the return data just say "print_r" so I have been trying foreach...
foreach ($result as $key => $value) {
echo "Key1: $key; Value2: $value<br />\n";
}
or
foreach ($results as $result) {
foreach ($result as $key => $value) {
echo "Key1: $key; Value2: $value<br />\n";
}
}
I have tried various other ways , I have looked over Stackoverflow and found 2 questions asking what I want but the questions were deemed off-topic ??? and closed.
Can anyone here help me with a way to ready the data into an array called config and the data be retrievable by config['Sitename']
Thank you