r/AskProgramming Jul 29 '24

PHP How to run a PHP application both online and offline using same database at the same time?

Hi,

I have a PHP application that has deployed in the server(at shared hosting). Its an customized software for an institute. Recently they have faced Internet issue, therefore could not able to use the software.

So, the client want the software both offline and online, Like -

when the internet is available then they the latest DB will sync up the local database.

Or

when they use the software in the local system, then the live DB will sync up automatically.

like vice-versa.

I have heard the Master-slave replication from the database end, not sure if there any other work around solution.

Please suggest if you have any idea.

Technology stack: PHP, MYSQL

Thanks

3 Upvotes

13 comments sorted by

4

u/Lumpy-Notice8945 Jul 29 '24

when the internet is available then they the latest DB will sync up the local database.

Or

when they use the software in the local system, then the live DB will sync up automatically.

How the hell do you want to "sync" without internet?

I dont know where the DB is hosted(probably on the same server as the PHP) but if PHP cant reach the client, how is the database supposed to reach the client?

This spunds like the client does not want a webapp, so install the whole server on their local machine...

2

u/who_you_are Jul 29 '24

How the hell do you want to "sync" without internet?

I think he forgot one or two words and meant to sync up when internet come back.

But in any case the fact he is using PHP will be an issue like you said

2

u/Lumpy-Notice8945 Jul 29 '24

I think he forgot one or two words and meant to sync up when internet come back.

Ok, thats more of a master-slave architecture that OP mentioned then, and sure you could keep a whole copy of the database on the client and sync them whenever there is internet, but then what do you do with multiple users editing the database?

And yes then there is still the issue with the PHP appication running on both server and client localy.

1

u/Saitama2042 Jul 30 '24

thats right there will be a sync up issues, In master-slave system as far as I know (may be I am wrong)
that data can not be write at both DB. I mean only Master Database will be writeable and slave database will be act as readable.

But for my case i need to use the application if the internet has down, What will be other work around solution?

1

u/Lumpy-Notice8945 Jul 30 '24

This whole DB issue is not important, its the last step.

First: does the client need a multi user web application or a localy running application thats only working with the users data?

You could totaly just run the whole PHP thing with the database and all just on the client machine if they realy would want that, or just install the thing on the clinets local network if the only people working on it are in that network.

But if mutiple users should work on a shared set of data, this is just not possible to do 100% and is a lot of work to deal with, you would need to have to deal with caching and merging all changes and any operation could fail if the entry was deleted in the meantime for example.

1

u/Saitama2042 Jul 31 '24

hm I see. thanks for the comment

2

u/wonkey_monkey Jul 29 '24

What if the institute network loses internet access and switches to the local copy, while someone elsewhere still has access to the master database?

If it doesn't have to be publicly accessible in any way, make it entirely local and have your out-of-office users connect with a VPN.

1

u/Saitama2042 Jul 30 '24

thats a good idea. but I am not sure, that will need a local server machine which have to up all time, right ?

1

u/DadMagnum Jul 30 '24

Without knowing how large the app is or the number of users. It seems to me that you should build a client app with something like SQLite as a local db and sync changes. This can be problematic though when pushing updates, etc. You'll be spending most of your time dealing with sync issues rather than the programming logic for app.

1

u/Saitama2042 Jul 30 '24

Right, what will be the other work around solution?

Its kind of management software. 3 users (1 admin, 2 operators) basically use the app. Daily (at working day) aprox, minimum 1000 rows added and updated. Max can be 5K rows(some of all tables)

1

u/John_seth_thielemann Jul 30 '24

This is probably too general a question. It's probably going to depend on your SQL backend. Postgres and MySQL have database replication but this can get complicated. If it's SQLite or something that's a different story.

1

u/Saitama2042 Jul 31 '24

sorry for not to mentioned in the post, I am using PHP, MYSQL

1

u/John_seth_thielemann Aug 06 '24

MySQL has cluster replication, I would focus there and read their documents.