Hi Iβm trying to update one of my tables which Iβve just added 4 new fields. I want to fill the fields with random numbers. I have got other fields in the table with data I need and can not loose
here is my code, im guessing βsetβ update all the fields with the same, is there a quick fix to change on field at a time with each random number generated?
I am getting random numbers but it is filling in every field for say ran1 with the same random number. I thought when the while loop, loops it would generate a new random number for the next row in my table.
Obviously thatβs not working if all of your values end up being the same. (j/k)
The reason why all of your rows have the same value is because you have no WHERE clause in your SQL. You literally said update every ran1 value to be 5 (or whatever value php chose randomly). To see this, echo your query β it should make much more sense what you are telling MySQL to do.
What you should do is use RAND() inside of the MySQL query. MySQL will give a random value for each row since you are calling the RAND function instead of passing a constant number.
Additionally, if your query is as simple as you show in your first post, you can even combine the 4 queries into one to make it super fast:
What is the exact query you are running to produce your random values? Did you seed your RAND() with id like r937 suggested?
And to use a min and max for your random numbers, use this (from the MySQL docs):
To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j - i)). For example, to obtain a random integer in the range the range 7 <= R < 12, you could use the following statement:
Sorry guys iβm not too good with all this php/MySql stuff.
This is my understanding, using php function rand() I get a number between the values I wish and assign them to a variable. I then use this variable in the query to add the random number to my field.
For some reason when i go to phpmyadmin all 1200ish row have the same number for said field. I thought the while loop would allow me to run the php function each time meaning a different number would appear in each row.