Automatic addition to the database of a million records. That not done right? Will help to understand?

$db = mysql_connect ("localhost","root","");
mysql_select_db ("test",$db);

$z = mysql_query("SELECT * FROM t", $db);
$rows = mysql_num_rows( $z );

echo $rows;

$i = $rows;
while ($i = 1000000) {
 $rz = mysql_query ("INSERT INTO t(ticket_id) VALUES(rand()*1000000)");

};

$start = microtime(true);
# ...
$time = microtime(true) - $start;
printf('<br>the Script is %.4F seconds.', $time);


Add in a dB table as long as the number will not be a million.
Unfortunately, it is added without the condition. Adds and adds.
The truth is very long and falls somewhere to 700 thousand. then fatal error sob.(in the browser writes) mysql is not configured. all works by default. Open Server 5.2.2.

I would be very grateful for the help and any thoughts about it. :) Or, if you are very bored, too, write. :)
SAP
July 8th 19 at 12:06
3 answers
July 8th 19 at 12:08
Solution
Functions mysql_* are generally outdated
while ($i = 1000000) is the same as while (true)
you don't know what version of php he's using ! - Caesar.Keeli commented on July 8th 19 at 12:11
if not 4, deprecated ) - amparo_Hickle commented on July 8th 19 at 12:14
: Yes, and 5 that are not deprecated - Caesar.Keeli commented on July 8th 19 at 12:17
Prompt how correctly to write that was added to until will not be a million in the database. does not work :( - Rose.Legros commented on July 8th 19 at 12:20
:
$i = $rows;
while ($i < 1000000) {
$rz = mysql_query ("INSERT INTO t(ticket_id) VALUES(rand()*1000000)");
$i++;
}; - amparo_Hickle commented on July 8th 19 at 12:23
: Thank you very much! :) it... could not understand why did not believe :) forgot to add :) - Rose.Legros commented on July 8th 19 at 12:26
I think the author of the question really need to be practice and study of the theory , the basics of PHP need to know . - sally_Carro commented on July 8th 19 at 12:29
: Just one question, why when running, 79760 total, Query took 0.0003 sec. in the query put 80000. do not get. up to one hundred thousand too. in General, all large did not finish. errors not issue. a little makes the rules - Rose.Legros commented on July 8th 19 at 12:32
: Few will correct you:
$i = 0;
while ($i < 1000000) {
$rz = mysql_query ("INSERT INTO t(ticket_id) VALUES(rand()*1000000)");
$i++;
};

either
$i = $rows;
while ($i > 0) {
$rz = mysql_query ("INSERT INTO t(ticket_id) VALUES(rand()*1000000)");
$i--;
};
- Daphne_Roob commented on July 8th 19 at 12:35
: no need to correct me, it is necessary to do this procedure ) - amparo_Hickle commented on July 8th 19 at 12:38
July 8th 19 at 12:10
First, you have a while loop must be a condition, and you have an assignment.

Second, try to make batches of 300-400 thousand.

Thirdly, you can in php.ini is a limit for script execution time and this limit is exceeded, so the script is worse.
Condition assignment. Do not understand, study, but I think that's a problem. we need to understand. Thank you. - Caesar.Keeli commented on July 8th 19 at 12:13
July 8th 19 at 12:12
Here is an option which should almost fly tweaked to pdo:
ini_set('mysql.connect_timeout', 10000);//here you can experiment
$start = microtime(true);//this gallop believes we complied with the code and so it should be on the top
//$db = mysql_connect ("localhost","root","");
//mysql_select_db ("test",$db);
try {$bd=new PDO("mysql:host=localhost;dbname=test",$user,$pass,//I think here it is clear what needs to be replaced
array(PDO::ATTR_ERRMODE =>PDO::ERRMODE_WARNING));
}catch(PDOException $e){echo "#mysql_pdo #1 Fatal error! Not correct data for PDO(MYSQL) connection!";exit;}
//$z = mysql_query("SELECT * FROM t", $db);
//$rows = mysql_num_rows( $z );
$rows=$bd->query('SELECT count(*) FROM t')->fetch(PDO::FETCH_ASSOC)['count(*)'];
echo $rows;
$rz=";//create a string variable
for($i=$rows;$i<1000000;i++) {
 $rz.=("INSERT INTO t(ticket_id) VALUES(rand()*1000000);");//write the query in place
};
//mysql_query ($rz);//one request is sent to all
$bd->exec($rz);
unset($rz);//free the memory
echo '<br>the Script ',microtime(true) - $start, 'seconds';//th WEA horror was here?

$db = mysql_connect ("localhost","root","");
mysql_select_db ("test",$db);

$start = microtime(true);
$z = mysql_query("SELECT * FROM t", $db);
$rows = mysql_num_rows( $z );
echo $rows;
$rz=";
for($i=$rows;$i<1000000;$i++) {
$rz.=("INSERT INTO t(ticket_id) VALUES(rand()*1000000);");
};
$time = microtime(true) - $start;
printf('
The script is %.4F seconds.', $time);

?>

Returns 0 rows. something not so.. doesn't pass the request. - Caesar.Keeli commented on July 8th 19 at 12:15
: I fixed it should work.. just edited the code... - amparo_Hickle commented on July 8th 19 at 12:18
: Warning: mysql_query(): MySQL server has gone away in C:\OpenServer\domains\messall.loc\index.php on line 13

Warning: mysql_query(): Error reading result set's header in C:\OpenServer\domains\messall.loc\index.php on line 13

Swears by it.. mysql_query ($rz);//one request is sent to all

:) - Caesar.Keeli commented on July 8th 19 at 12:21
this variant is to optimize and optimize. - Rose.Legros commented on July 8th 19 at 12:24
: Everything is possible :) but I am very interested and most importantly instructive. people worked hard and offered an interesting option :) thanks. - amparo_Hickle commented on July 8th 19 at 12:27
: www.mysql.ru/docs/man/Gone_away.html
The most often error MySQL server has gone away is the result of a timeout of the connection and it was closed by the server. By default, the server closes the connection after 8 hours of inactivity. You can change the time limit setting when starting mysqld wait_timeout variable.

Another common reason for receiving error MySQL server has gone away is to issue the command "close" on the MySQL connection, and then attempts to execute a query on a closed connection.
ie even so not enough time to MySQL serv to learn the details. - Rose.Legros commented on July 8th 19 at 12:30
: corrected response - sally_Carro commented on July 8th 19 at 12:33
: The server is working. I work with the database and stuff :( although I may not understand correctly, after reading - Rose.Legros commented on July 8th 19 at 12:36
: I corrected the answer - try to use it - Daphne_Roob commented on July 8th 19 at 12:39
: Thank you very much! Errors does not produce, but the database says nothing. - amparo_Hickle commented on July 8th 19 at 12:42
: there's just too large the request is received. Better allow querying the
INSERT INTO t(ticket_id) VALUES (1),(2),(3)... etc
keeping it in the stream to a file and then feed this fight muscle, using exec. - Rose.Legros commented on July 8th 19 at 12:45
: the table is empty again? - Rose.Legros commented on July 8th 19 at 12:48
this should not depend on.. this is the time of optimization which have so far. - Josiane75 commented on July 8th 19 at 12:51
: it shouldn't, but it depends. All didn't fit in the buffer of the request and here's the problem =)
There the text turns to a few tens of megabytes. - Rose.Legros commented on July 8th 19 at 12:54
: I'm not a specific need to fill, namely to add 500 or more data. Here. for this I used your script. there are no errors but the table is not populated. :( - Haley0 commented on July 8th 19 at 12:57
: : try Lam 1000 to replace. if it works, correct answer. - Rose.Legros commented on July 8th 19 at 13:00
Changed to 100. 0 results is written to the database - Josiane75 commented on July 8th 19 at 13:03
: not going to work. then there's the problem ;
It does not work for mysql_query, the current in the PDO rolls. - Josiane75 commented on July 8th 19 at 13:06
: then most likely an error in the query itself Your. try replacing mysql_query ($rz); echo $rz; and manually fill in what happens. - Rose.Legros commented on July 8th 19 at 13:09
it is quite possible so long as php 7.0 sitting - Josiane75 commented on July 8th 19 at 13:12
: 0 the results in the database. The function performed 10-fold.INSERT INTO t(ticket_id) VALUES(rand()*1000000);INSERT INTO t(ticket_id) VALUES(rand()*1000000);INSERT INTO t(ticket_id) VALUES(rand()*1000000);INSERT INTO t(ticket_id) VALUES(rand()*1000000);INSERT INTO t(ticket_id) VALUES(rand()*1000000);INSERT INTO t(ticket_id) VALUES(rand()*1000000);INSERT INTO t(ticket_id) VALUES(rand()*1000000);INSERT INTO t(ticket_id) VALUES(rand()*1000000);INSERT INTO t(ticket_id) VALUES(rand()*1000000);INSERT INTO t(ticket_id) VALUES(rand()*1000000);
The script is 0.0029051303863525 sec

ini_set('mysql.connect_timeout', 100000000);//here you can experiment
$start = microtime(true);//this gallop believes we complied with the code and so it should be on the top
$db = mysql_connect ("localhost","root","");
mysql_select_db ("test",$db);
$z = mysql_query("SELECT * FROM t", $db);
$rows = mysql_num_rows( $z );
echo $rows;
$rz=";//create a string variable
for($i=$rows;$i<10;$i++) {
$rz.=("INSERT INTO t(ticket_id) VALUES(rand()*1000000);");//write the query in place
};
echo 'Function executed '.$i.'times';
echo $rz;//one request is sent to all
unset($rz);//free the memory
echo '
The script is ',microtime(true) - $start, 'seconds';//th WEA horror was here?
?> - Rose.Legros commented on July 8th 19 at 13:15
: pho supports the server? - Haley0 commented on July 8th 19 at 13:18
*pdo - Josiane75 commented on July 8th 19 at 13:21
That is, the function is executed anyway. but since the string is empty.. maybe because of this? :) guess the ranks. heh - Josiane75 commented on July 8th 19 at 13:24
: pdo server supports? - Haley0 commented on July 8th 19 at 13:27
: Yes - Rose.Legros commented on July 8th 19 at 13:30
: then it is better to rewrite on it.. can? - Josiane75 commented on July 8th 19 at 13:33
Unfortunately no :( I'm just learning. coming up with interesting puzzles and trying to figure it out, please help people :) who then do not mind, time, nerves, etc :) and by looking at the code storm books already on the move to figure out what's what. here. - Rose.Legros commented on July 8th 19 at 13:36
try it - Haley0 commented on July 8th 19 at 13:39
: Tried cool. Works, but there are some strange bugs, for example when entering the page, and create conditions to 100, adds only 1938 records. when you refresh the page (run) added 189, with the next update added 500 with something of the records in the database. in General, in some randomness without a certain sequence. That is not the condition to pick up and pour from some 100k records. - Josiane75 commented on July 8th 19 at 13:42
try to do echo and see what the end's going on. - Josiane75 commented on July 8th 19 at 13:45
Tried, everything seems to be in reasonable aisles. but don't pour all the figures. pour 250K rows in the database is always less than 220.. Pour 100k added 80-90, etc. I Just became very interesting, because you said that this method will just fly :) - Rose.Legros commented on July 8th 19 at 13:48
: most likely the limit of strings in php. parties to transfer then you need simple.. I worked there where 100 queries a lot and used this optimization. - Josiane75 commented on July 8th 19 at 13:51
In any case, thank you very much for your help and time spent. I will try :) to find a way :) - Josiane75 commented on July 8th 19 at 13:54

Find more questions by tags PHP