I have a web page where people are able to post a single number between 0 and 10.
There is like a lotto single number generation once daily. I want my PHP script to check on the the posted numbers of all the users and assign a score of +1 or -1 to the relative winners (or losers).
The problem is that once I query the DB for the list of the winning users, I want to update their “score” field (in “users” table). I was thinking of a loop like this (pseudocode)
foreach winner{
update score +1
}
but this would mean that if there are 100 winners, then there will be 100 queries. Is there a way to do some sort of batch inserting with one single query?
Thanks in advance.
Let’s just assume we have a datatable named
postsandusers.Obviously,
userscontain the data of the gambler (with a convenientidfield andpointsfor the number of points they have), andpostscontain thepost_idID field for the row,user_id, which is the ID of the user andvalue, the posted number itself.Now you only need to implement the following SQL queries into your script:
UPDATE users INNER JOIN posts ON users.id = posts.user_id SET users.points = (users.points + 1)WHERE posts.value = 0;
Where
0at the end is to be replaced with the randomly drawn number.What will this query do? With the
INNER JOINconstruct, it will create a link between the two tables. Automatically, ifposts.valuematches our number, it will linkposts.user_idtousers.id, knowing which user has to get his/herpointsmodified. If someone gambled0, and his ID (posts.user_id) is8170, thepointsfield will update for the user havinguser.id = 8170.If you alter the query to make it
(users.points - 1)andWHERE posts.value != 0, you will get the non-winners having one point deducted. It can be tweaked as much as you want.Just be careful! After each daily draw, the posts table needs to be truncated or archived.
Another option would be storing the timestamp (
time()in PHP) of the user betting the number, and when executing, checking against the stored timestamp… whether it is in between the beginning and the end of the current day or not.Just a tip: you can use graphical database software (like Microsoft Access or LibreOffice Base) to have your
JOINs and such simulated on a graphical display. It makes modelling such questions a lot easier for beginners. If you don’t want desktop-installed software, trying out an installation ofphpMyAdminis another solution too.Edit:
Non-relational databases
If you are to use non-relational databases, you will first need to fetch all the winner
IDs with:This will give you a result of multiple rows. Now, you will need to go through this result, one-by-one, and executing the following query:
(
0is the drawn winning number,1is the concurrentidof the user to update.)Without using the relation capabilities of MySQL, but using a MySQL database, the script would look like this:
The
whileconstruct make this loop to run until every row of the result (list of winners) is updated.Oh and: I know MySQL is a relational database, but it is just what it is: an example.