foreach ($users as $User) { $servername = "internal-db.s127112.gridserver.com"; $username = "db127112_david"; $password = "thomas123"; $dbname = "db127112_timetracking"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $currentmonth = date('F Y'); $stmt = $db->prepare("SELECT * FROM billing_rate WHERE uid=?"); $stmt->execute(array($User['uid'])); $isCurrentMonth = $row["month"] != $currentmonth; $stmt = $db->prepare("SELECT * FROM billing_rate WHERE uid=? AND month=?"); $stmt->execute(array($User['uid']), $currentmonth); $result = $stmt->fetchAll(); if( $isCurrentMonth && mysql_num_rows($result) == 0) { $stmt = $db->prepare("INSERT INTO billing_rate (uid,month,hourly_rate, monthstart, monthfinish) VALUES(:uid,:month,:hourly_rate, :monthstart, :monthfinish)"); $stmt->execute(array( ':uid' => $User['uid'], ':month' => date('F Y'), ':hourly_rate' => '1250', ':monthstart' => date('Y-m-01'), ':monthfinish' => date('Y-m-t') )); } }