简体   繁体   中英

How to replace empty query with value in PHP + MySQL

I need to replace returned query from MySQL, if it is empty, with zero, but I can't figure out how.

For example I have table named values . There is device id and some period with values.

id_device | 2015-05-01 | 2015-05-02 | 2015-05-03 | 2015-05-04 | 2015-05-05
---------------------------------------------------------------------------
     1    |    1000    |    990     |    980     |    970     |    960    |
     2    |    1150    |    1140    |    1130    |    1120    |    1100   |
     3    |    1050    |    1040    |    1030    |    1020    |    1010   |
     4    |            |            |            |            |           |
     5    |    1250    |    1240    |    1230    |    1220    |    1210   |

When I use

$sql = mysql_query("SELECT * FROM values");
while ($row = mysql_fetch_array($sql)) {
    if (mysql_num_rows($row) == 0) { 
         $row[1] = 0; $row[2] = 0; $row[3] = 0; $row[4] = 0; $row[5] = 0;
         }
    echo "<td>$row[1] $row[2] $row[3] $row[4] $row[5]</td>";
    }

The 4th row is still empty instead of being replaced by zeros. Why? And how can I replace the empty fields with zeros?

You can handle that in your SQL Query, See example below:

SELECT `device_id`, IF(`column_name` = '', 0, `column_name`) as col_name FROM `table_name`

Apart from that, mysql_num_rows is used to return number or rows, in executed query.

EDIT:

You can use IFNULL , just incase if you are getting null values instead of blanks.

The problem here is your condition, the mysql_num_rows . It checks, how many rows your SQL query returned. It returns this number, no matter where you are in your code, until your resource object ( $sql ) is overwritten.

So what your code does is the following:

#execute the sql statement
$sql = mysql_query("SELECT * FROM values);
#get the rows from the sql result, on by one
#the first $row contains id_device #1 and so on
while ($row = mysql_fetch_array($sql)) {
    #here you look up the number of rows your sql statement gets
    #this always returns 5, as the result from your query does not
    #change when looking at the single lines of your result
    if (mysql_num_rows($row) == 0) { 
        #you'll never enter this condintion
        $row[1] = 0; $row[2] = 0; $row[3] = 0; $row[4] = 0; $row[5] = 0;
    }
    echo "<td>$row[1] $row[2] $row[3] $row[4] $row[5]</td>"
}

Try this instead:

$sql = mysql_query("SELECT * FROM values);
while ($row = mysql_fetch_array($sql)) {
    #checks if one of the cells is empty
    if (in_array("", $row)){
        #if so, $row will be filled with empty values
        #except for the first element, as this is your device_id
        $row = array_fill(1, count($row), 0);
    }
    echo "<td>$row[1] $row[2] $row[3] $row[4] $row[5]</td>"
}

You are checking if the number of rows are equal to zero but I understand that you want to check if there is empty columns, the MySQL will return empty string if the column is NULL or empty in the database, so you can simply add inside the while loop a check for each column if it is empty to become equal with zero

eg

if($row[1]=="") $row[1]=0;
if($row[2]=="") $row[2]=0;
if($row[3]=="") $row[3]=0;
if($row[4]=="") $row[4]=0;
if($row[5]=="") $row[5]=0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM