2017-05-30 22 views
0

我希望有一個簡單的問題,我找不到任何答案,我的問題是完整的。PHP數組到無序列表

我從MySQL返回的數組,看起來像這樣

mysql> select c.comment, d.name, m.ounces from drink d 
     inner join mix m on m.drinkID = d.ID 
     inner join cocktail c on m.cocktailID = c.id 
     where c.id =5; 
+----------------------+------------+--------+ 
| comment    | name  | ounces | 
+----------------------+------------+--------+ 
| Long Island Iced Tea | Vodka  | 0.75 | 
| Long Island Iced Tea | Gin  | 0.75 | 
| Long Island Iced Tea | Rum  | 0.75 | 
| Long Island Iced Tea | Tequila | 0.75 | 
| Long Island Iced Tea | Sour Mix | 2.00 | 
| Long Island Iced Tea | Cola  | 1.00 | 
| Long Island Iced Tea | Triple Sec | 1.00 | 
+----------------------+------------+--------+ 

我試圖讓PHP產生類似下面這樣一個無序列表。

<div class="jumbotron"> 
    <h2> 
     Long Island 
    </h2> 
    <p> 
     <ul> 
     <li>0.5 oz Vodka</li> 
     <li>0.5 oz Rum</li> 
     <li>0.5 oz Gin</li> 
     <li>0.5 oz Tequila</li> 
     <li>0.5 oz Triple Sec</li> 
     <li>1 oz Sweet and Sour Mix</li> 
     <li>1 oz cola </li> 
     </ul> 

我是想這樣的:

$ingredient = "select 
     c.comment, d.name, m.ounces 
     from drink d 
     inner join mix m on m.drinkID = d.ID 
     inner join cocktail c on m.cocktailID = c.id 
     where c.id = 4"; 
    //print($ingredient); 
    $result = mysql_query($ingredient); 

<?php foreach($result as $results): ?> 
    <li><? echo $results ?></li> 
<?php endforeach; ?> 

從這裏:StackOverflow但無法弄清楚,爲什麼我一直從PHP接收HTTP 500錯誤。

任何幫助或指導,將不勝感激!

謝謝!

+2

不要使用'mysql_ *'函數。自v5.5(2013年6月)開始,它們已被棄用,並從v7.0(2015年12月)開始刪除。請使用[** mysqli _ ***](https://secure.php.net/manual/en/book.mysqli.php)或[** PDO **](https://secure.php.net /manual/en/book.pdo.php)與[**準備語句**](https://secure.php.net/manual/en/pdo.prepare.php)和[**綁定參數** ](https://secure.php.net/manual/en/pdostatement.bindparam.php)。 –

+0

通常500是語法錯誤 – clearshot66

+0

謝謝。我正在重複使用另一篇文章的代碼。 mysqli_ *函數是否可以被找到並替換,或者它們是非常不同的? – user31238633

回答

0

您正在使用短標籤其中最有可能禁用返回一組行,而不是一個單獨的行。試試這個

<li><?= $results['ounces'] . ' oz ' $results['name"]?></li>

-1

mysql_query()

<?php while ($row = mysql_fetch_assoc($result)): ?> 
    <li> <?= $row['ounces'] ?> oz <?= $row['name'] ?></li> 
<?php endwhile ?> 
+0

希望mysql_query返回一個錯誤。而已。沒有什麼。 – Strawberry

+0

在循環播放結果之前,希望能夠測試一個錯誤。我想我應該補充說 – Jaime