0

異なるテーブルから現在の日付でレコードをカウントし、新しいテーブルの異なる列を持つ 1 つの行として返したいと考えています。コードは 3 時間ごとにレコードを更新し、現在の日付が変わると新しいレコードを挿入します。「created_at」列に現在の日付と時刻のデータ (2013-05-20 14:12:12) があります。ここに私の現在のコード:

require_once('./db_connect.php');
$dbcon = new db;

//test to see if a specific field value is already in the DB 
public function in_table($table,$where) {
  $query = 'SELECT * FROM ' . $table . ' WHERE ' . $where;
  $result = mysqli_query($this->dbh,$query);
  $this->error_test('in_table',$query); 
  return mysqli_num_rows($result) > 0;
}

//running in background
while (true) {
   $select= "SELECT (SELECT CURDATE()) AS time," .
           "(SELECT COUNT(tweet_id) FROM tweets WHERE created_at= 'CURDATE() %') AS total_count," .
           "(SELECT COUNT(fid) FROM fun WHERE ftime= 'CURDATE() %') AS f_count," .
           "(SELECT COUNT(sid) FROM sad WHERE stime= 'CURDATE() %') AS s_count";

    $results = mysqli_query( $dbcon, $select );

    while($row = mysqli_fetch_assoc($result)) {
       $time = $row['time'];
       $total = $row['total_count'];
       $fcount = $row['f_count'];
       $scount = $row['s_count'];

       $field_values = 'time = "' . $time . '", ' . 'total_count = ' . $total . ', ' . 'fun_count = ' . $fcount . ', ' . 'sad_count = ' . $scount;

       if ($dbcon->in_table('count','time= "' . $time . '"')) {
         $update = "UPDATE count SET $field_values WHEN time= '$time'";
         mysqli_query( $dbcon, $update );
       }
       else {
         $insert = "INSERT INTO count SET $field_values";
         mysqli_query( $dbcon, $insert );
       }
   }

   //update record every 3 hour
   sleep(10800);
}

このコードでは、カウント レコードを取得できません。結果は| 2013-05-18 | 0 | 0 | 0 |. どうすればこれを修正できますか?

4

3 に答える 3