Mysql database queries in a loop

It is necessary that records from 5 to 1455 be entered into the database in a certain table.
I do this:

<?
$dbhost = "localhost"; // 
$dbuser = "user"; //  
$dbpassword = "pass"; // 
$dbname = "dbname"; //   
$link = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbname, $link);
$x = 5;
while (x < 1455):
$query = "insert into b_user_group values(".$x.",'8','NULL', 'NULL')";
$x++;
endwhile;
mysql_query($query, $link);
mysql_close($link);?>

But it doesn’t work for some reason. What could be wrong?


Answer 1, authority 100%

$x = 5;
$query = "insert into b_user_group values ";
$comma = 0;
while (x < 1455) {
  if ($comma) { $query .= ', '; } else $comma++;
  $query .= '('.$x.',\'8\',NULL, NULL)';
  $x++;
  }
$query .= ';';
mysql_query($query, $link);

Answer 2, authority 75%

You form a query in a loop, but execute it outside the loop. Once.


Answer 3, authority 25%

the while loop should look like this:

while ($x < 1455):
$query = "insert into b_user_group values('".$x++."','8','NULL', 'NULL')";
mysql_query($query, $link);
endwhile;

Answer 4, authority 25%

<?php
$dbhost = "localhost"; // 
$dbuser = "user"; //  
$dbpassword = "pass"; // 
$dbname = "dbname"; //   
$link = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbname, $link);
$x = 5;
$values = '';
while ( x < 1455 ) {
  if ( empty($values) ) {
    $values .= '(' . $x . ',8,"NULL","NULL")';
  } else {
    $values .= ',(' . $x . ',8,"NULL","NULL")';
  }
  $x++;
}
$sql = 'INSERT INTO `b_user_group` VALUES ' . $values;
mysql_query($sql, $link);
mysql_close($link);