2014-02-06 145 views
-3

我需要顯示數據庫中的公司名稱,但此代碼對我無效。請幫忙。由於在php中顯示來自mysql的數據結果

<?php 

// connect to the database 
include('php/db.php'); 

    $result = mysql_query("select name from company where company_id = 1"); 


echo $result['name']; 
?> 
+1

是的,讓我做一些神奇找出什麼在你的數據庫中。那麼,你的'mysql_fetch_array()'順便說一下? –

+0

我的代碼現在看起來像這樣,但沒有任何結果: <?php //連接到數據庫 include('php/db.php'); $ result = mysql_query(「select name from company where company_id = 1」);同時($ row = mysql_fetch_array($ result)) { echo $ row ['name']。「
」; } ?> –

回答

0

試試這個

// connect to the database 
include('php/db.php'); 
$result = mysql_query("select name from company where company_id='1'"); 
while($row = mysql_fetch_array($result)) 
{ 
     echo $row['name']."<br>"; 
} 
+0

我試了一下代碼,沒有任何結果。你可以在www.bigbossfood.com/companyname.php 看到結果的任何其他想法? –

+0

另一個你可以使用mysql_fetch_assoc返回適當的對象之後,你可以在嘗試使用foreach循環顯示後將數據存儲到array()中。 –

0

試試這個代碼:

while($row = mysql_fetch_array($result)) 
    { 
    echo ""+$row['name']; 
    echo "<br>"; 
    } 
-1

mysql_query返回一個對象,你首先需要把對象變成使用關聯數組mysql_fetch_assoc();

然後可以數組訪問元素使用您使用的方法。

您可能想要查看使用mysqli函數,因爲mysql函數已折舊。