dongmaopiao0901 2016-10-19 19:51
浏览 35

从数据库中的列创建数组,我可以在html中回显

Im sorry if this has been answered before but I am new to PHP and MySQL and I can't figure this out.

Pretty much every time I alter my code to include an array I get a fatal error. What I am trying to do is display all the data in 3 columns from my table.

I have my site set up where you log in and I store that user's name as a "code" in a session. I have a table that has multiple user form entries that are differentiated by the user's code because in my form, I grab the code as a hidden field and add it to the entry in the table.

So far I have been able to isolate those entries by the users code, in one column I have the sum of all of the user's numerical data and I am able to echo this as a total.

I want the other 3 columns to display all the values in their columns and for each value have a line break in between them. And I am trying to print or echo these results in specific parts on a confirmation page.

I have seen examples with PDO using fetch_all and other examples of storing arrays but I can't seem to figure it out with my existing code.

Here is my existing code:

<?php
$user = *****;
$pass = *****;
$dbh = new PDO('mysql:host=localhost;dbname=*****', $user, $pass);

$stmt = $dbh->prepare("SELECT sum(price),part_number,location,price FROM products WHERE code = :usercode");
$stmt->bindParam(':usercode', $_SESSION['MM_Username']);

if ($stmt->execute()) {
$user = $stmt->fetch(PDO::FETCH_ASSOC);

}

?>

And here is where I want to display the results:

<table style="margin:0 auto;" cellspacing="7" width="100%">
<tbody>

<tr>
  <td><?php echo $user['part_number']; ?></td><!--all column values-->
  <td><?php echo $user['location']; ?></td><!--all column values-->
  <td><?php echo $user['price']; ?></td><!--all column values-->
  <td><?php echo "Total:",  $user['sum(price)']; ?><br></td><!--this is ok-->

</tr>


 </tbody>
</table>
  • 写回答

5条回答 默认 最新

  • dourenzhou8740 2016-10-19 19:55
    关注

    Change to this

    <? echo 
    

    to

    <?php echo
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c