English 中文(简体)
回回/产出0(零)不起作用
原标题:Echo / Output 0 (zero) is not working
  • 时间:2012-05-25 04:59:20
  •  标签:
  • php
  • echo

我在运行一个查询以从数据库表格中获取一个PDF文件中显示的几个小时的总和,但由于某种奇怪的原因,它不会回声 0 (0) 。

 $query = mysql_query("SELECT id, SUM(hours_night), SUM(hours_days) FROM table WHERE invoiceID= ".mysql_real_escape_string($invoiceID)." ") or die(mysql_error());
 $result = mysql_num_rows($query);

 $totalhours_night = 0;


     while ($fetch = mysql_fetch_assoc($query)) {

         $totalhours_night += $fetch[ SUM(hours_night) ];
     }


$html_output = "Some html and tables markup... " . $totalhours_night . "";

html 的输出效果良好, 问题不在于问题。 但是问题在于它会出于某种原因输出 0( 零) 。 如果小时结果是实际的, 类似 1 或 5 或 其它什么, 它会输出总小时的罚款, 但我需要它来输出 0, 如果没有小时 。

因为如果没有时间的话 什么都不做看起来很奇怪, 我需要显示零,因为这样看起来更好看。

BTW, 举例来说, 如果我放数字_format( $tall hours_ night, 2); 它确实显示 0.00, 但我需要它只是 0 。

问题回答

三点:

  1. Your SQL query is wrong, you can t display id and SUM(..) unless you use GROUP BY id (Which kid of loses the idea if id is unique)
  2. If you use SUM(..) give it an alias like SUM(..) AS alias1, and later in the PHP refer to alias1.
  3. If this is not the reason, try var_dump($fetch[ alias1 ]); and look at what you really get back. (And post your findings here so we can give you further help)

将整数作为字符串处理, 像这样-

echo (string)$totalhours_night;




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签