0

$result = mysql_query("SELECT * FROM rooms");
while($row = mysql_fetch_array($result))
{
mysql_query("insert into chat (name,text,date,chat_id) values('$_SESSION[username]','$_POST[text]','$date','$row[id]')");
}

can i do that without repeat the mysql_query code?

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by amear
0

Yes, you just build up a 'values' string and execute a single query:

$result = mysql_query("SELECT * FROM rooms");
while($row = mysql_fetch_array($result)){
  $r[] = "('" . mysql_real_escape_string($_SESSION['username']) . "','" . mysql_real_escape_string($_POST['text']) . "','" . $date . "','" . $row['id'] . "')";
}
$valuestring = implode(",",$r);
$insert = mysql_query("INSERT INTO `chat` (`name`,`text`,`date`,`chat_id`) VALUES ($valuestring);

Not tested

Edited by diafol: n/a

0

Yes, you just build up a 'values' string and execute a single query:

$result = mysql_query("SELECT * FROM rooms");
while($row = mysql_fetch_array($result)){
  $r[] = "('" . mysql_real_escape_string($_SESSION['username']) . "','" . mysql_real_escape_string($_POST['text']) . "','" . $date . "','" . $row['id'] . "')";
}
$valuestring = implode(",",$r);
$insert = mysql_query("INSERT INTO `chat` (`name`,`text`,`date`,`chat_id`) VALUES ($valuestring);

Not tested

its doesn't work thank u for your trying ,, i really desperate

0

Yes, you just build up a 'values' string and execute a single query:

$result = mysql_query("SELECT * FROM rooms");
while($row = mysql_fetch_array($result)){
  $r[] = "('" . mysql_real_escape_string($_SESSION['username']) . "','" . mysql_real_escape_string($_POST['text']) . "','" . $date . "','" . $row['id'] . "')";
}
$valuestring = implode(",",$r);
$insert = mysql_query("INSERT INTO `chat` (`name`,`text`,`date`,`chat_id`) VALUES ($valuestring);

Not tested

omg i fix the code it will be like this


$result = mysql_query("SELECT * FROM rooms");
while($row = mysql_fetch_array($result)){
$r[] = "('" . mysql_real_escape_string($_SESSION) . "','" . mysql_real_escape_string($_POST) . "','" . $date . "','" . $row . "')";
}
$valuestring = implode(",",$r);
mysql_query("INSERT INTO `chat` (`name`,`text`,`date`,`chat_id`) VALUES $valuestring");


thank u a lot its working

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.