username123 Posted March 9, 2009 Share Posted March 9, 2009 hi guys i need help here as im really new to php and mysql. lets say i have a - database, - one php script updating this database at certain moments of time. i wanted to run this script from command line for an infinite period of time so it would behave sort of like a server. - another php script used to communicate with the user. this script is supposed to retrieve data from database and maybe request an update of certain rows in it. now to the question when the 'server' is doing its calculations and updating the database i want the other script (the one communicating with user) to wait with certain reads until 'server' has finished its job. 1) How do i do that? 2) How could those 2 scripts communicate? i know i could lock certain rows in the database until 'server' has finished with them but what are the other options here? how about setting some flags or creating an array with identifiers of rows being currently updated? How could those scripts share these data. tnx Quote Link to comment https://forums.phpfreaks.com/topic/148582-communication-between-php-scripts-how/ Share on other sites More sharing options...
DjMikeS Posted March 9, 2009 Share Posted March 9, 2009 I would use a session variable.... When the script is updating the db, set the variable. When it is finished, unset it. In the other script, you could just check if that session variable exists and if it does, display an error or whatever... Quote Link to comment https://forums.phpfreaks.com/topic/148582-communication-between-php-scripts-how/#findComment-780231 Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 A session wouldn't work if the update is running from the command line. Sessions are created from the browser per user. As you say use a flag field in the table. Before it starts updating set the flag (i.e. recordLock) on all rows it is about to update: UPDATE table SET recordLock = '1' WHERE ..... Reset the flag when a record is updated or reset with 1 query at the end of the update UPDATE table SET recordLock = '0' WHERE recordLock='1' The other script cannot view rows that are locked SELECT * FROM table WHERE recordLock !='1' Quote Link to comment https://forums.phpfreaks.com/topic/148582-communication-between-php-scripts-how/#findComment-780239 Share on other sites More sharing options...
username123 Posted March 9, 2009 Author Share Posted March 9, 2009 tnx mike and neil. well, i want to make that script with user communication to wait until it can read the data. i really dont want to send any error msgs or anything like "please try again" is my only option to put it into a loop for as long as these rows are locked? doesnt sound like a good idea to me. i've read somewhere about sockets and it seemed to me it could be good idea to arrange communication between those 2 scripts through them. i thought i could make the other script to ask 'server' through a socket if certain entities from my database can be retrieved. unfortunately as i understand if i run socket_listen() on my so called 'server' it'll just sit there waiting for connection and its not what i want, after all its main purpose is to run frequent updates, not having constant lunch break. any sugestions guys? Quote Link to comment https://forums.phpfreaks.com/topic/148582-communication-between-php-scripts-how/#findComment-780277 Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 No, your thought process is not on the lines of how PHP works. Scripts do not communicate with each other. Script 1 does a specific job Script 2 does a specific job and is independent of script 1. Like I said if script 1 is running in the background (i.e. run from the command line) then script 2 which is accessed from a web browser by a user request cannot read into script 1 and say where are you up to? Script 2 gets rows from the database to display to the user, correct? If you do not want to return rows that script 1 is updating then a simple flag on these records will act as the condition in the WHERE claus of the SQL that script 2 uses. Quote Link to comment https://forums.phpfreaks.com/topic/148582-communication-between-php-scripts-how/#findComment-780298 Share on other sites More sharing options...
username123 Posted March 9, 2009 Author Share Posted March 9, 2009 If you do not want to return rows that script 1 is updating then (...) well actually i WANT script 2 to return those rows but in case those rows are being updated i want script 2 to WAIT until they're processed. the problem is how do i force script 2 to wait. if i flag rows in question then to make script 2 wait i would have to go into a loop and query the database in every iteration checking if these rows are finally unflagged. am i right or do i miss something? my idea was, let's say if script 1 is updating all rows with userID=blahblah, to create an array with userID's currently being updated and put blahblah in there rather than to flag 500000 rows. it should be a lot more efficient. checking if blahblah is being updated also should be faster this way. of course we're coming to the problem of sharing this array between scripts. No, your thought process is not on the lines of how PHP works. Scripts do not communicate with each other. well yes, it certainly seems that php doesnt do a lot to help me in this department. however i know i still can arrange some sort of data exchange between them either by - sockets - shared memory segment or - file neil: so you're saying flagging rows still would be the fastest solution here? just asking as i havent dug into details on sockets or shared memory segment yet. and before any1 asks i dont have any code written here yet. im just at concept stage. tnx Quote Link to comment https://forums.phpfreaks.com/topic/148582-communication-between-php-scripts-how/#findComment-780387 Share on other sites More sharing options...
Mark Baker Posted March 9, 2009 Share Posted March 9, 2009 If your database of choice supports row-level locking, this is not an issue Quote Link to comment https://forums.phpfreaks.com/topic/148582-communication-between-php-scripts-how/#findComment-780388 Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 What is it exactly that you are updating? Why would you need to update 500000 rows? Quote Link to comment https://forums.phpfreaks.com/topic/148582-communication-between-php-scripts-how/#findComment-780406 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.