2012-11-30 78 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(); 
?>