我想从MySQL数据库中获取一个多维数据库数组,我希望将一个字段转换为浮点数。
我使用的是PHP 5.3和MySQL 5
这是我的代码:
***我的PDO课程:
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 */
}
}
我这样使用它:
$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);
和:
$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);
这是这些变量的var_dump:
$ 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
$数据类型:
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)
我不知道没有什么问题不能让' 字段浮动???