I want to get a multidimentional php array from MySQL database where I want to cast a field to float.
I'm using PHP 5.3 and MySQL 5
and this is my code:
*** My PDO class:
class DB {
private static $instance = NULL;
public static function getInstance() {
if (!self::$instance) {
try {
self::$instance = new PDO("mysql:host=" . SERVER . ";dbname=" . DtB, USR, PASS);
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
self::$instance->query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
} catch (PDOException $e) {
echo 'Échec lors de la connexion : ' . $e->getMessage();
}
}
return self::$instance;
}
private function __clone() {
/* interdiction de cloner l'instance */
}
}
And I use it like this:
$datas1 = $db->query('
SELECT
r.nom AS nomReg,
COUNT(e.regions) AS nbr
FROM regions r
LEFT OUTER JOIN event e ON r.nom=e.regions
GROUP BY r.nom
')->fetchAll(PDO::FETCH_ASSOC);
and :
$dataTypes=$db->query("
SELECT tt.nomtype_tache AS name,
CAST(((COUNT(t.nomtype_tache) / (
SELECT count(t.idtype_tache) AS y
FROM type_tache tt
LEFT OUTER JOIN tache t
ON tt.idtype_tache = t.idtype_tache
)
) * 100) AS DECIMAL(5,1)) AS y
FROM type_tache tt
LEFT OUTER JOIN tache t ON tt.idtype_tache = t.idtype_tache
GROUP BY name
")->fetchAll(PDO::FETCH_ASSOC);
and this the var_dump of these variables:
$datas1:
array (size=4)
0 =>
array (size=2)
'nomReg' => string 'ARIANA' (length=6)
'nbr' => int 1
1 =>
array (size=2)
'nomReg' => string 'BEJA' (length=4)
'nbr' => int 0
2 =>
array (size=2)
'nomReg' => string 'BEN AROUS' (length=9)
'nbr' => int 1
3 =>
array (size=2)
'nomReg' => string 'BIZERTE' (length=7)
'nbr' => int 0
$dataTypes:
array (size=3)
0 =>
array (size=2)
'name' => string 'type1' (length=5)
'y' => string '0.0' (length=3)
1 =>
array (size=2)
'name' => string 'type2' (length=5)
'y' => string '0.0' (length=3)
2 =>
array (size=2)
'name' => string 'type3' (length=5)
'y' => string '13.3' (length=4)
I don't no what is the problem that don't make the 'y' field to be float???