I need help with what I'm trying to do, my scripting knowledge is very low so any help will be appreciated!
Short description of my script,
it's designed to look up every user of my site that has put their streaming detail for a streaming site and check if they are online or offline, once it finds out it updates the online/offline field.
Now I setup cron to run the script every 5-10minutes so it would stay updated, which isn't the problem.
The problem I'm having is that now that more and more people got added to the database, the script takes longer to finish and puts lots of strain on the cpu.
I'm very beginner at writing php and just put it together with reading tutorials so I'm pretty sure I coded it poorly.
Is it your hosting provider who is saying that the CPU usage is too high? Are you on shared hosting?
Although I've never used that Xfire API that you're using, nothing looks to far out of line to me. How many users are being added to the script each time the cronjob runs?
1) Your code is jumping in and out of the php processor all the time. i.e. ?> followed by <?php get rid of those.
2) You have a function defined inside of a loop. Move the function outside of the loop. Put at the top of your code.
3) You are using very inefficient code with mysql_result() which gets only one value at a time. Would be much more efficient with mysql_fetch_array(). Fill the array, then process the array.
<?php
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-EU; rv:1.8.1.9) Gecko/20071025 Firefox/3.6.15');
mysql_connect("localhost","username","password");
mysql_select_db("databasename") or die("Unable to select database");
$resultx=mysql_query("SELECT username, xfire FROM users");
if(mysql_errno()) die(mysql_error());
if(strpos($ratingtext, 'Unknown' )){ //Nice use of type juggling :) I like it!
echo "<font color='#FF0000'>Offline</font>";
mysql_query("UPDATE users SET xfirecheck ='0' WHERE xfire = '".mysql_real_escape_string($f8)."'");
} else {
echo "<font color='#00FF00'>Online</font>";
mysql_query("UPDATE users SET xfirecheck ='<font color=#00FF00>Online</font>' WHERE xfire = '$f8'");
}
}
}
mysql_close(); //also nice ;)
?>
Thats so much easier to read. I know I broke it in a couple places (I could hardly translate what you coded). The last query is probably broken, also-- it makes no sense having echo "online | offline" without a username to go with it
I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;
Bookmarks