N
N
Name2018-03-19 18:00:45
PHP
Name, 2018-03-19 18:00:45

Write a random sample of strings to the database?

There are 5 lines
1
2
3
4
5
how to write a script correctly to write random 3 lines to the database

mysql_query("UPDATE table SET text = '$peremennaya');

what should be assigned here - $peremennaya?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-03-19
@User782

You can for example like this:

$strings = ['one', 'two', 'three', 'four', 'five']; //массив строк
$p_array = []; // массив выбранных строк
for ($i = 0; $i < 2; $i++) {
  //Элементы не будут повторятся. Если нужно, чтобы элементы повторялись - нужно юзать array_rand
  $p_array[] = pop(array_shuffle(&$strings)); //собственно выбираем случайный элемент
}
$peremennaya = implode("\n", $p_array); //имплодим через нужный разделитель

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question