Code: Select all
<?php
$rand = mt_rand(1,10);
$day = date("j");
$query = mysql_query("UPDATE table SET num=num+$rand WHERE day!='$day'");
?>But I need to UPDATE different numbers (1,10) in all rows.
What is the solution?
Thank You in advance.
Moderator: General Moderators
Code: Select all
<?php
$rand = mt_rand(1,10);
$day = date("j");
$query = mysql_query("UPDATE table SET num=num+$rand WHERE day!='$day'");
?>You have assigned a randomly selected number to the variable $rand. But you have used that fixed number in your query, so it is doing exactly what you asked it to do.woohoogf wrote:When I write this code and test, rand take number and update in all rows the same number.Code: Select all
<?php $rand = mt_rand(1,10); $day = date("j"); $query = mysql_query("UPDATE table SET num=num+$rand WHERE day!='$day'"); ?>
But I need to UPDATE different numbers (1,10) in all rows.
woohoogf wrote:Thank You so much for helping.
But I'm having problem with understand what is written in the link.
Sorry me for this. But I'm a little newbie.
So if it's not hard for someone, please write it like in php file like I wrote.
With that way I'll understand.
Thank You in advance.
Applying this to your problem, it should be something like this:MySQL Manual: wrote: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:
SELECT FLOOR(7 + (RAND() * 5));
Code: Select all
<?php
$day = date("j");
$query = mysql_query("UPDATE table SET num = num + FLOOR(1 + RAND()*9) WHERE day <> '$day'");
?>califdon wrote:woohoogf wrote:Thank You so much for helping.
But I'm having problem with understand what is written in the link.
Sorry me for this. But I'm a little newbie.
So if it's not hard for someone, please write it like in php file like I wrote.
With that way I'll understand.
Thank You in advance.Applying this to your problem, it should be something like this:MySQL Manual: wrote: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:
SELECT FLOOR(7 + (RAND() * 5));Code: Select all
<?php $day = date("j"); $query = mysql_query("UPDATE table SET num = num + FLOOR(1 + RAND()*9) WHERE day <> '$day'"); ?>
Code: Select all
FLOOR(1 + RAND()*9) Code: Select all
<?php
$rand = rand(1,12)
if ($rand > 8 )
{
$rand = 9;
}Thank you very much for explaining this all.FLOOR(3.14159) is 3, FLOOR(127.4) is 127. The RAND() function returns a pseudo-random floating point number that is between 0 and 1. If you multiply RAND() by some integer, you will get a floating point number between 0 and that integer. So the expression above will return an integer between 1 and 10 (1 + 9).
[Edit: Now that I look at that again, it is unlikely to ever produce 10 because that's the extreme upper limit. In order to get the value 10 to occur, you'd probably need to change the 9 to a 10.]
I'm afraid you're not explaining WHY you want to make the "average" (which I don't think really even applies here) any particular number. The whole point of a random selection is to not have any pattern to the results. I think maybe what you are thinking of is that you want there to be an equal chance for any particular selection to occur. That's exactly what a random choice does for you. There isn't any concept of "average".woohoogf wrote:I want to make something like referral bot system, but not referral bot.
I need that script for my work which works like referral bot in PTCs.