php和mysql中的数学

时间:2013-08-17 08:11:45

标签: php mysql math

我试图通过在数据库中使用入口在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 ();

1 个答案:

答案 0 :(得分: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> ";