我有这段代码,可以从 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 为空?表中有3条记录.. 你能帮我吗?
最佳答案
您没有将结果存储在任何合适的位置。看看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
}
}
WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_fetch_array() PDOStatement::fetch()
关于mysql_query 返回 0 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26087275/