mysql - PHP script lagging my website -
Then I have this script (which is important for the website) and it runs every 3-5 minutes
Actually, what is this script, it receives information from the API request and updates the table with new API requests.
The problem is, it runs lots of API requests (about 500) because there are 500 entries in my table.
And for that reason, my CPU Uses up to 100%. How can I avoid this? How can I limit the CPU usage of this script alone?
header ('content-type: text / html; charset = UTF-8'); Date_default_timezone_set ('America / Los_Angeles'); Echo memory_get_usage () "\ N"; $ Con = mysqli_connect ("localhost", "***", "***", '***'); $ Result = mysqli_query ($ con, "SELECT * FROM ** _ stream"); While ($ row = mysqli_fetch_array ($ result)) {$ json_array = json_decode (file_get_contents ('https://api.twitch.tv/kraken/streams/'rstrolower ($ line [' channel ']), true); If they are going to prevent variables ($ json_array ['stream']! = Null) {// out of SQL injection $ displayname = mysqli_real_escape_string ($ con, $ json_array ['stream'] ['channel'] ['display_name' Change in); $ Title = mysqli_real_escape_string ($ to $ json_array ['stream'] ['channel'] ['position']); $ Game = mysqli_real_escape_string ($ con, $ json_array ['stream'] ['channel'] ['sports']); $ Viewers = mysqli_real_escape_string ($ con, $ json_array ['stream'] ['viewers']); $ Preview = mysqli_real_escape_string ($ con, $ json_array ['stream'] ['preview'] ['medium']); $ Followers = mysqli_real_escape_string ($ con, $ json_array ['stream'] ['channel'] ['follower']); $ Date = date ('m / day / y h: i: s', time ()); Mysqli_query ($ con, "SET NAMES utf8mb4"); Mysqli_query ($ con, "UPDATE mybb_streams SET` online` = '1',` title` = '$ title', `viewers` = '$ viewers',` sports` =' $ game ', `preview` =' $ Preview ', `Followers` =' $ Followers ',` Last Action` =' $ date 'where' channel '=' ". ($ Line ['channel'])". ") Or die (" a MySQL Error & lt; br / & gt; Your query: UPDATE `stream` SET` online` =` 1`, `title` =` $ title`, `viewer` =` $ audience`, `game` =` $ game `,` Preview` = `$ preview` where channel = '" .strtolar ($ line [' channel ']).' '
Root: ("Mysqli_errno ($ con.") ". Mysqli_error ($ thief));} Other {mysqli_query ($ con," UPDATE mybb_streams SET` online` = '0', `Viewer` = '0' where 'Channel' = '".strtolar ($ line [' channel '])" "" or die ("a MySQL error has occurred.
Your query: Update streams online SET` online' = '0', `Viewer '=' 0 'where' channel '='" .strtolower ($ row ['channel']). ''
Error: ("Mysqli_errno ($ con ) ")". Mysqli_error ($ con));}} echo memory_get_usage () "\ N"; ? & Gt;
Database refresh!
Slowly. For each stream you have a) HTTP request api.twitch.tv (which prevents php execution of all, until the response from the API is not returned) then you execute two questions.
I just saw the API and if you call before, it will give you a list of all the rivers I'm guessing.
Then get a single call to get all the streams loop through API results and update your database in that way.
Comments
Post a Comment