Forum Moderators: open

Message Too Old, No Replies

comparing variables

check value against a pair of variables

         

wheelie34

9:21 am on Sep 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all

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

tomda

9:30 am on Sep 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep!

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'" );
$checking = mysql_fetch_row($result);
or
$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

wheelie34

9:40 am on Sep 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Tomda

I have changed * to count(id)

It still falls over on the if line, the error is

unexpected T_BOOLEAN_AND

it doesnt like one of the &&

tomda

9:48 am on Sep 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I didn't post the other error

if ( $checking <1 ) && ( $time >= 12 && $time <= 13 )
should be
if ( $checking <1 && ( $time >= 12 && $time <= 13 ))

BTW, makee sure you have a field named id in your database table to use count(id).
Tomda

wheelie34

10:48 am on Sep 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks again Tomda

cant test it yet as my DNS provider just died, uk 123reg

will post if it worked or not when they decide to fix their DNS

wheelie34

1:11 pm on Sep 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



had to use num_rows as fetch_row was returning an array

it now works fine thanks for the help