php - 如何在PHP网站中显示MySQL数据库数据?

标签 php html mysql database display

我有:My current view

我想要的是:6 items next to each other from the same database

不幸的是,我之前的问题没有得到我想要的答案,所以这是另一个问题。

代码:

<?php # DISPLAY COMPLETE PRODUCTS PAGE.

# Access session.
session_start() ;

# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }

# Set page title and display header section.
$page_title = 'Shop' ;
include ( 'includes/header.html' ) ;

# Open database connection.
 require ( '..\connect_db.php' ) ;

# Retrieve items from 'shop' database table.
$q = "SELECT * FROM shop" ;
$r = mysqli_query( $dbc, $q ) ;
if ( mysqli_num_rows( $r ) > 0 )
{



   # Display body section.
   echo '<UL>
        <li><a href="shop_details.php?id=1">View Item</a></li>
        <li><a href="shop_details.php?id=2">View Item</a></li>          
        <li><a href="shop_details.php?id=3">View Item</a></li>
    </UL>
    ';


    #Displays every item in the store. 
  echo '<table><tr>';

$formatting = 0;
   while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC ))
   {
      echo $formatting;
      if ($formatting = 6){
         echo '<td><strong>' . $row['item_name'] .'</strong><br><span     style="font-size:smaller">'. 
$row['item_desc'] . '</span><br><img src='. $row['item_img'].' height=200px><br>$' . $row['item_price'] . 
'<br><a href="shop_details.php?id='.$row['item_id'].'">View Item</a></td></tr><tr>';
        $formatting = 0;
     }
     else 
     {
         echo $formatting;
         echo '<td><strong>' . $row['item_name'] .'</strong><br><span style="font-size:smaller">'. 
$row['item_desc'] . '</span><br><img src='. $row['item_img'].' height=200px><br>$' . $row['item_price'] . 
'<br><a href="shop_details.php?id='.$row['item_id'].'">View Item</a></td>';
         $formatting = $formatting + 1;
}
     }
 }


   echo '</tr></table>';

      # Close database connection.
    mysqli_close( $dbc ) ; 
  }
  # Or display message.
   else { echo '<p>There are currently no items in this shop.</p>' ; }

   # Create navigation links.
   echo '<p><a href="cart.php">View Cart</a> | <a href="forum.php">Forum</a> | <a href="home.php">Home</a> | <a href="goodbye.php">Logout</a></p>' ;

     # Display footer section.
     include ( 'includes/footer.html' ) ;

    ?>

感谢您的帮助。

最佳答案

我对使用迭代的 while 循环有点困惑.. 像这样使用它,更干净..正确获取所有行^_^

$result = $dbc->query("SELECT * FROM shop");
while($row = $result->fetch_assoc()){
    echo "$row['item_name']";
}

关于php - 如何在PHP网站中显示MySQL数据库数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42441133/

相关文章:

php - 打开像页面一样的 href 到特定位置

php - 使用 MVC 在 zend 框架中包含页面页眉/页脚的最佳方法

php - 如何将我插入的密码与 php 中的 md5 哈希进行比较

javascript - 防止 jQuery 跳到底部(使用 fadeIn 时)

javascript - 填充 HTML 下拉列表 onclick

javascript - 使用jquery从数组和html列表中删除项目

MySQL:优化无主键的表(索引、外键)

php - Laravel artisan 路线 :call Throws MySQL Error

mysql - 从 db 获取简单信息到 node.js http 服务器响应

mysql - 将两个 60,000 行表连接在一起并获得 1000 万行?