0

我试图通过在文件中显示它们来使用我的数据库中的条目在 php 中进行简单的求和。除了总和之外,所有其他字段都完美显示

    <?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT make, id, model, bcosts, rcosts, value, id FROM referb';

mysql_select_db('ely');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
    echo "<tbody><tr class=\"odd\">

        <td>{$row['make']} </td>".
         "<td>{$row['model']} </td> ".
         "<td>{$row['id']}</td> ".
         "<td>{$row['bcosts']}</td> ".
         "<td>{$row['value']}</td> ".
         "<td>{$row['rcosts']}</td> ".
         "<td> £ 

$first_number = {$row['bcosts']};
 $second_number = {$row['rcosts']};
 $third_number = {$row['value']};

$sum_total = $third_number - $second_number - $first_number;

print ($sum_total);


 </td> ";
} 

?>

我得到的输出是

£ = 100; = 25; = 300; = - - ; print ();
4

1 回答 1

0

这是您要打印的字符串:

"<td> £ 

$first_number = {$row['bcosts']};
$second_number = {$row['rcosts']};
$third_number = {$row['value']};

$sum_total = $third_number - $second_number - $first_number;

print ($sum_total);


</td> ";

变量被它们的值替换:

"<td> £ 

 = 100;
 = 25;
 = 300;

 =  -  - ;

print ();


</td> ";

并且浏览器在显示时会将多个空白字符转换为一个空格。

您无法在字符串内部计算内容。这应该解决它:

"<td> £"; 

$first_number = $row['bcosts'];
$second_number = $row['rcosts'];
$third_number = $row['value'];

$sum_total = $third_number - $second_number - $first_number;

print ($sum_total);


echo "</td> ";
于 2013-08-17T08:17:03.527 回答