我需要顯示數據庫中的公司名稱,但此代碼對我無效。請幫忙。由於在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'];
?>
我需要顯示數據庫中的公司名稱,但此代碼對我無效。請幫忙。由於在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'];
?>
試試這個
// 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>";
}
我試了一下代碼,沒有任何結果。你可以在www.bigbossfood.com/companyname.php 看到結果的任何其他想法? –
另一個你可以使用mysql_fetch_assoc返回適當的對象之後,你可以在嘗試使用foreach循環顯示後將數據存儲到array()中。 –
試試這個代碼:
while($row = mysql_fetch_array($result))
{
echo ""+$row['name'];
echo "<br>";
}
mysql_query
返回一個對象,你首先需要把對象變成使用關聯數組mysql_fetch_assoc();
然後可以數組訪問元素使用您使用的方法。
您可能想要查看使用mysqli
函數,因爲mysql
函數已折舊。
是的,讓我做一些神奇找出什麼在你的數據庫中。那麼,你的'mysql_fetch_array()'順便說一下? –
我的代碼現在看起來像這樣,但沒有任何結果: <?php //連接到數據庫 include('php/db.php'); $ result = mysql_query(「select name from company where company_id = 1」);同時($ row = mysql_fetch_array($ result)) { echo $ row ['name']。「
」; } ?> –