我的代码从MySql表中获取所有记录并执行开关状态:
<?php
$DB_host = 'localhost';
$DB_user = 'user';
$DB_password = 'pwf';
$DB_name = 'dbname';
$connessione = mysql_connect($DB_host, $DB_user, $DB_password);
$db_obj = mysql_query("SELECT * FROM `dbname`.`table`")or die("Query not valid: " . mysql_error());
$i = 0;
foreach ($db_obj as $dato) {
$i++;
switch ($dato->turno) {
case 0:
$desc = 'Mattina';
$col = 'bg-color-redLight';
break;
case 1:
$desc = 'Pomeriggio';
$col = 'bg-color-greenLight';
break;
case 2:
$desc = 'Notte';
$col = 'bg-color-blueLight';
break;
case 3:
$desc = 'Smonto Notte';
$col = 'bg-color-yellow';
break;
case 4:
$desc = 'Riposo';
$col = 'bg-color-orange';
break;
}
}
?>
为什么结果$ db_obj是empy?在表中有3条记录.. 你能帮帮我吗?
答案 0 :(得分:0)
您不会将结果存储在任何合适的地方。看看mysql_fetch_array
$db_obj = mysql_query("SELECT * FROM `dbname`.`table`")or die("Query not valid: " . mysql_error());
$i = 0;
while($datato = mysql_fetch_array($db_obj, MYSQL_ASSOC)) {
$i++;
switch ($dato['turno']) {
//Cases
}
}
警告:自PHP 5.5.0起,此扩展程序已弃用,将来将被删除。相反,应使用MySQLi或PDO_MySQL扩展名。另请参阅MySQL:选择API指南和相关的常见问题解答以获取更多信息。该功能的替代方案包括: mysqli_fetch_array() PDOStatement对象::取()