需要帮助向网页显示mysql数据库内容

时间:2012-11-30 23:44:03

标签: php mysql

我需要帮助将数据从mysql显示到网页,我在php中编码。

我的数据库包含汽车产品(相同类型,如雪佛兰),现在我有2行(如果我愿意,我可以添加更多),每辆车都包含图像路径和说明。

我可以显示一行(汽车),但我无法显示所有行。我知道我必须通过一个循环来获取汽车数据库中的所有数据,但我不知道如何实现它。

这是我到目前为止所拥有的。假设我已连接到我的数据库 注意:图像路径我想在我的网站上显示图片。

我希望它能在我的网页中显示:

    $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>";

3 个答案:

答案 0 :(得分:0)

您在循环中拼错了$ numberOfFields,这意味着您正在为循环控件使用不同的变量。你的循环不起作用。

我建议打开错误报告,以便PHP可以为您捕获这些内容。

答案 1 :(得分:0)

只需添加一个while循环。 mysqli_fetch_assoc返回一行并将内部指针移动到下一行,直到获取所有行,然后返回false并且while循环将停止

要理解的伪语法

while ( this is true ) {
     execute this
}

所以在你的情况下,你可以说

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

mysqli_fetch_assoc和mysqli_fetch_row字面意思相同,assoc为您提供了结果字段名称作为索引的数组,因此在使用fetch_row时,这更容易访问($ row ['name']而不是$ row [0]

玩得开心! :)

修改

// 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 );

答案 2 :(得分:0)

使用此功能......只需循环

<?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();
?>