Forum Moderators: open
I am trying to add weather details to a db, the weather page gets the details from an xml feed and prints them, now I would like to add them to a db so they can be displayed as historical data.
The weather page gets over 500 visits per day so I want to capture the data around noon when the page loads between 1200 and 1300 hours.
Heres the code so far
$time = date("H");#works fine prints hour
$dblink=mysql_connect($dbhost, $dbuser, $dbpass)or die("System down!");
mysql_select_db($dbbase, $dblink)or die("Database down!");
$result=mysql_query("select * from weather where date='$date'" );
$checking = mysql_fetch_row($result); // check if db already has entry
if ( $checking <1 ) && ( $time >= 12 && $time <= 13 ) // if no entry in db and time between 12pm and 1pm
{
$sql="insert into weather values (' ', '$temperature', '$icon', '$outlook', '$sunrise', '$sunset', '$winddirection', '$humidity', '$dewpoint', '$date', '$bar', '$bar2')";
}
else
{
// do nothing
}
mysql_close($dblink);
It fails at the if function, can anyone see the problem? thanks
It fails because ($checking < 1) is wrong since your checking doesn't return the number or rows.
To return number of rows, either use
$result=mysql_query("select count(id) from weather where date='$date'" );or
$checking = mysql_fetch_row($result);
$checking=mysql_num_rows($res);
There is a difference between mysql_fetch_rows, mysql_num_rows!
Lastly, avoid to use * in your query when not necessary.
Tomda