How do I store a date using MySQL?

I want to save a date in a server and I think that using MySQL is the way, but I don’t really know how to apply it. I have this code:

public DateTime fechaHoy = System.DateTime.Today;
public DateTime fecha = new DateTime(2019, 12, 24, 0, 0, 0);
public RawImage evento;

void Start()
{
  if(fechaHoy == fecha)
  {
  evento.gameObject.SetActive(true);
  }
}

When it’s the day of “fecha”, an image is shown. What I want to do is to change the date from a server and send it to the client (the DateTime in the code is just an example, I have to change it to get the value from the server). What’s the easiest way to do this? Thank you.

You’ll be needing a MySQL library or DB connection library; and remember that calls will be asynchronous.

I’m not sure what you’re planning. Databases don’t “send” anything to clients. They respond to requests. If you want something to update this database you’ll want a server build of some kind, though doing a simple update to a date field in a database could be made a simple as a shell script called by a cron job.

Clients could request from the database through a PHP web server interface, or if you went with a server side Unity build the clients could connect to that server using most any of the available network API’s. Though if you went that route, storing a single date in a database is a bit overkill since you could just write it to playerprefs on the server.

2 Likes

@FVelasco besides you need setup server with SQL and PHP, you need use web request from Unity.
This is not only solution, but in most cases sufficient.

Why a database?

You could trivially easily have a text file with a date in it on a website. Use the WWW class to fetch the text file and read it. Job’s done.

Databases are useful if you have lots of data, or if data contains relationships, or if there are rules to enforce, or other complex use cases. If all you need to do is share some values there are much easier ways.

4 Likes

I started off using databases, but moved to keeping all characters and world objects as data files as I had no need for data relationships. The difference in speed and resource usage was ridiculously large.

Never talk directly to a SQL server from a client. Use a rest service. The database needs to be hidden behind firewalls. There are a ton of reasons why you shouldn’t directly let the clients talk to the database, from security to versioning.

1 Like

Me: Select star from potato table.

Waits

Me now screaming: Select star from potato table!!

Mumbles Still nothing. [client looks at me funny]

Client: Maybe you need to use a rest service?

Me: Sure, kicks off shoes, lies back on couch and requests a Frappacino . . . I’ll try again in five minutes!

1 Like

Yeah you are right, that seems simpler. I only want to change the date remotely and the program has to read and use the values, so I’m going to search how to do what you have said.

It seems that the WWW class is obsolete.

I’m sure a quick search will reveal its replacement. :wink:

2 Likes