Hi i have a small query which takes data from one mysql database and inserts it into a duplicate table, the problem is i get the relevant data out of the database fine, but i can not seem to get it in to the new database, i have error reporting enabled, but i am not getting any errors.
Below is my code:
$dbhost = "";
$dbuser = "";
$dbpass = "";
$dbname = "";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database");
mysql_select_db($dbname);
$result = mysql_query("SELECT * FROM ip_stats WHERE DATE(date) = CURDATE()")
or die(mysql_error());
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
$id = $row['id'];
$date = $row['date'];
$ip = $row['ip'];
$hostname = $row['hostname'];
$company_name = $row['company_name'];
$address = $row['address'];
$descr = $row['descr'];
echo "$id - $date - $ip - $hostname - $company_name - $address - $descr";
?>
<br />
<?php
$query = "INSERT INTO `ip_stats11` (`id`, `ip`, `date`, `hostname`, `company_name`, `address`, `descr`, `updated`)
VALUES ('$id', '$ip', '$date', '$hostname', '$company_name', '$address', '$descr', 'yes')";
echo "updated $ip"; ?><br /><?php
}
@klijn is probably right that you forgot to execute the
INSERTquery.However, I would advise to do this simpler and faster:
One other thing – this construct:
Might not be very efficient. You apply a function to the date column. If that is of the
DATEdatatype, you don’t need that and you should leave it out. If it is aDATETIMEand you want to truncate the time part, then doing it this way will make it impossible for mysql (and most other RDBMS-es too) to use any indexes on that column.Here’s a better way:
The way this works is that
CURRENT_DATEwill be cast to a DATETIME (assuming thedatecolumn is aDATETIMEorTIMESTAMP, and it will have a zero timepart, indicating the start of that day and the start of our BETWEEN…AND range. For the end of the range, we say: please use tomorrow:Which is in fact the first second of the 1 day later than current_date. This is not entirely correct, because
BETWEEN...AND...uses inclusive range boundaries, so we substract one second with the- MINUS 1 SECONDto ensure we got the very last second of the day indicated by CURRENT_DATE.