Cita:
y me gustaria optimizar mi codigo.. ya que tengo un for que me hace 36 querys.... se notara la diferencia?Avoid doing SQL queries within a loop
A common mistake is placing a SQL query inside of a loop. This results in multiple round trips to the database, and significantly slower scripts. In the example below, you can change the loop to build a single SQL query and insert all of your users at once.
Produces:
Produces:
A common mistake is placing a SQL query inside of a loop. This results in multiple round trips to the database, and significantly slower scripts. In the example below, you can change the loop to build a single SQL query and insert all of your users at once.
Código PHP:
foreach ($userList as $user) {
$query = 'INSERT INTO users (first_name,last_name) VALUES("' . $user['first_name'] . '", "' . $user['last_name'] . '")';
mysql_query($query);
}
Código:
Instead of using a loop, you can combine the data into a single database query.INSERT INTO users (first_name,last_name) VALUES("John", "Doe")
Código PHP:
$userData = array();
foreach ($userList as $user) {
$userData[] = '("' . $user['first_name'] . '", "' . $user['last_name'] . '")';
}
$query = 'INSERT INTO users (first_name,last_name) VALUES' . implode(',', $userData);
mysql_query($query);
Código:
INSERT INTO users (first_name,last_name) VALUES("John", "Doe"),("Jane", "Doe")...
alguien me podria ayudar....
Pdta: el codigo es muy largo asi que lo pasaria por email...
gracias