dongtongjian3127 2013-12-05 18:57
浏览 35

使用双引号的MySQL SELECT在php中打印时遇到问题

If anyone has any ideas on how to help me out with this, when I run it usually just creates a never ending table.

<?php

$link = mysql_connect('localhost', 'blah', 'blah') or die('Could not connect: ' . mysql_error());

mysql_select_db('blah') or die('Could not select database');

extract($_GET);

error_reporting(error_reporting() & ~E_NOTICE );

$query = "SELECT `city`.`month`,`city`.`cost`, `comcast`.`cost`, `electric`.`cost`, `city`.`cost` + `comcast`.`cost` + `electric`.`cost` AS \"Total\" FROM `city`, `comcast`, `electric`";



    echo "<table border = '1'>

          <tr>

          <th>Date of Bills</th>

          <th>City Bills</th>

          <th>Comcast Bills</th>

          <th>Electric Bills</th>

          <th>Total Bills</th>

          </tr>";



$dave= mysql_query($query) or die(mysql_error());



while ($row = mysql_fetch_array($dave,mysql_ASSOC)) {

        echo "<tr>";

        echo "<td>" . $row[`city`.`month`] . "</td>";

        echo "<td>" . $row[`city`.`cost`] . "</td>";

        echo "<td>" . $row[`comcast`.`cost`] . "</td>";

        echo "<td>" . $row[`electric`.`cost`] . "</td>";

        echo "<td>" . $row[`\"Total\"`] . "</td>";

        echo "</tr>";





    }







//SELECT `city`.`month`, `city`.`cost`+ `comcast`.`cost`+ `electric`.`cost` AS "Total" FROM `city`, `comcast`, `electric`

// Free resultset

mysql_free_result($result);



// Closing connection

mysql_close($link);

?> 
  • 写回答

2条回答 默认 最新

  • dpj96988 2013-12-05 19:03
    关注

    replace ` with ' when displaying data:

    while ($row = mysql_fetch_array($dave,mysql_assoc)) {
    
        echo "<tr>";
    
        echo "<td>" . $row['month'] . "</td>";
    
        echo "<td>" . $row['costCity'] . "</td>";
    
        echo "<td>" . $row['costComcast'] . "</td>";
    
        echo "<td>" . $row['costElectric'] . "</td>";
    
        echo "<td>" . $row['Total'] . "</td>";
    
        echo "</tr>";
    }
    

    and change your query to:

    $query = "SELECT `city`.`month`,`city`.`cost` as costCity, 
                     `comcast`.`cost` as costComcast, 
                     `electric`.`cost` as costElectric, 
                     `city`.`cost` + `comcast`.`cost` + `electric`.`cost` AS \"Total\" 
                      FROM `city`, `comcast`, `electric`";
    

    Also I would look into JOINS and you are missing closing table tag </table>

    Since you are selecting all data from 3 tables without using joins it multiples so if your tables contain 2 rows in each you get result 2*2*2 = 8.

    NOTE: do not use mysql_* functions. Try using prepared statements PDO or MySQLi_*

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度