0

我需要帮助将数据从 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>";
4

3 回答 3

0

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

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

于 2012-11-30T23:50:02.873 回答
0

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

伪语法理解 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 );
于 2012-11-30T23:56:08.460 回答
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();
?>
于 2012-12-01T00:06:11.267 回答