0

I am trying to update all of the rows in a table in my database with a different value. I am trying to while loop the query to do this. Here's what I have...

<?php
$rand = rand(100,150000);
$start = 1;
$start += $start;
$start2 = $start +1;
echo $start;
while($start =< 686) {
echo "UPDATE table_video SET total_view = $rand WHERE id BETWEEN $start AND $start2;";
};
?>

I am sure most of you should be able to look at my code and understand what I am trying to accomplish. I would like the assistance. Thank You very much!

7
  • You only echo the query, never execute.. $rand also will only be generated once. All records will have the same integer on each iteration. You should put the assignment in the loop. Commented Feb 12, 2017 at 23:37
  • 1
    I'm trying to understand the $start thing... you initialise it to 1. Then you add 1 ($start = 2) and then $start2 is $start + 1 which is 3. (I don't know why you do all of that tbh. Then you make a while-statement that never ends because you're never increasing the $start (so it always is $start =< 686). Commented Feb 12, 2017 at 23:40
  • why you use a LOOP? instead just update all the columns with a random value? Commented Feb 12, 2017 at 23:52
  • What I want is for the first instance to echo UPDATE table_video SET total_view = 74956 WHERE id = 1; then UPDATE table_video SET total_view = 54687 WHERE id = 2; ... I made a mistake in my question, the BETWEEN was for a different trial and error. Commented Feb 12, 2017 at 23:52
  • @JuanCarlosOropeza how would I do that? Commented Feb 12, 2017 at 23:53

1 Answer 1

1

Use the mySQL RAND() function, min and max your range for the random values. I belive on this case are (100,150000)

UPDATE table_video 
SET total_view =  ROUND( (RAND() * (max-min)) +min )
WHERE id BETWEEN 1 AND 686;
Sign up to request clarification or add additional context in comments.

1 Comment

That worked. Thank You very much @JuanCarlosOropeza

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.