2012-11-30 79 views
0

我需要從mysql顯示數據到網頁的幫助,我在php中編碼。我的數據庫由汽車(同類型,如雪佛蘭)等產品組成,現在我有2排(如果需要,我可以添加更多),每輛汽車都包含圖像路徑和說明。需要幫助顯示mysql數據庫內容到網頁

我可以顯示一行(汽車),但我無法顯示所有行。我知道我必須經過一個循環才能從汽車數據庫獲取所有數據,但我不知道如何實現它。

這是我到目前爲止。假設我已經連接到我的數據庫 注意:我想在我的網站上顯示圖片的圖像路徑。

這是我怎麼想它在我的網頁上顯示:

$query = "SELECT * FROM cars where cars.carType = 'Chevy' AND \ 
    cars.active = 1"; 
    $numberOfFieds = mysqli_num_fields($result); 
    $numberOfRows = mysqli_num_rows($result); 

    /* Gets the contents */ 
    $row = mysqli_fetch_row($result); 
    $rows = mysqli_fetch_assoc($result); 
    $fieldcarssontable = array_keys($row); 



    echo "<table>"; 

    while($row = mysqli_fetch_assoc($result)){ 
    echo "<th>" . $fieldcarssontable[imgPath] . "</th>"; 
    echo "<th>" . $fieldcarssontable[description] . "</th>"; 

    } 


    echo "</tr>"; 

    echo "</table>"; 
+0

您的圖片不工作。看看'while'循環。 – Kermit

+0

您缺少$ with numberOfFieds = mysqli_num_fields($ result);並且請在echo''您的代碼'後面和結尾處添加'單引號'';或者你已經看到我的答案... –

回答

0

只需添加一個while循環。 mysqli_fetch_assoc返回行和內部指針移動到下一行,直到所有行被提取,則返回假,while循環將停止

僞語法來理解,而

while (this is true) { 
    execute this 
} 

因此,對你的情況可以說

while ($row = mysqli_fetch_assoc($result)) { 
    // process/output $row 
} 

mysqli_fetch_assoc和mysqli_fetch_row字面上做的一樣,assoc命令將讓你與你的結果字段名的數組索引,所以這是簡單的存取($行[「名稱」]而不是$行[0]當使用fetch_row時)

玩得開心! :)

編輯

// connect to your database server 
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db'); 

// an error occured 
if (!$link) { 
    die('Connect Error (' . mysqli_connect_errno() . ') ' 
     . mysqli_connect_error()); 
} 

// build your query 
$query = "SELECT 
       *  # select actual fields instead of * 
      FROM 
       cars 
      WHERE 
       cars.carType = 'Chevy' 
      AND 
       cars.active = 1"; 

// execute query 
$result = mysqli_query($link, $query); 

if (!$result) { 
    die('no result'); 
} 

// number of fields 
$numberOfFields = mysqli_num_fields($result); 

// the field names 
$fieldNames  = mysqli_fetch_fields($result); 

// number of result rows 
$numberOfRows = mysqli_num_rows($result); 

// watch the content of fieldName and compare it with the table header in the output 
print_r($fieldName); 

echo "<table>\n"; 

// table header, not neccessary to put this into a loop if the query isn't dynamic 
// so you actually know your field names - you can echo the header without any variable. 
// for the sake of learning about loops I added this 

foreach($fieldNames as $index => $fieldName) { 
    echo "\t<th>field #" $index . ", name:" . $fieldName . "</th>\n"; 
} 

// now it's time to walk through your result rows, since we only need to check for "true" a while loop does best 

while ($row = mysqli_fetch_assoc($result)) { 
    echo "\t<tr><td>" . $row['imgPath'] . "</td><td>" . $row['description'] . "</td></tr>\n"; 
} 

echo "</table>\n"; 

// remove the result from memory 
mysqli_free_result($result); 

mysqli_close($link); 
+0

我試過你的方式,但它不工作,我編輯我的問題 –

+0

首先確保你實際上得到的結果查詢,然後array_keys返回你想要的,但結果是用數字索引訪問的。使用print_r觀察它的內容以獲得更好的理解。你也可以刪除前兩個提取命令,while循環應該包裝實際的表格列,你只需要一次表格標題,所以在你的循環中使用mysqli_fetch_fields。我們不是在這裏寫你的代碼:) –

0

你在你的循環,這意味着你使用你的循環控制不同的變量拼錯$ numberOfFields。你的循環將無法工作。

我建議打開錯誤報告,以便PHP可以爲您捕獲這些東西。

+0

哦..謝謝,但我認爲,不會解決我的問題,雖然 –

0

使用此...只是while循環

<?php 
// Array 
while($result = mysql_fetch_assoc($result)) { 
     //show you fields 
     echo $result["FieldName"]; 
    } 
?> 

或者使用適當的

<?php 
// Edit it as per your query 
$query = "SELECT * FROM cars"; 

if ($result = $mysqli->query($query)) { 

    /* fetch associative array */ 
    while($row = $result->fetch_assoc()) { 
     //show you fields 
     echo $row["Name"]; 
    } 

    /* free result set */ 
    $result->free(); 
} 

/* close connection */ 
$mysqli->close(); 
?>