2014-09-28 79 views
0

我有這個代碼,從一個MySQL表獲取所有記錄,並執行開關stateform:的mysql_query返回0元

<?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

唉... **的mysql _ *** – 2014-09-28 16:59:45

回答

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 
    } 
} 

mysql_已被棄用,它的建議尋找到mysqlipdo

警告:這個擴展不贊成PHP 5.5.0,並且將被刪除未來。應該使用MySQLiPDO_MySQL擴展名。另請參閱MySQL:選擇API指南和相關FAQ以獲取更多信息。替代該功能包括: mysqli_fetch_array() PDOStatement對象::獲取()

+0

我已經在這條線的錯誤.. 的foreach($拿督= mysql_fetch_array( $ db_obj,MYSQL_ASSOC)){ – Joe 2014-09-28 17:13:12

+0

對不起,應該是'while'! – 2014-09-28 17:16:00

+0

好吧,它的作品!謝謝! – Joe 2014-09-29 12:38:11