Our blog

 

PHP Generated SQL

If you find yourself laboriously building SQL queries by typing each field = 'value' statement... think again.

Imagine this:

PHP:
  1. $sql_query = mysql_query("select * from table");
  2.  
  3. $insert_elsewhere_sql = "INSERT INTO other_table SET ";
  4.  
  5. while($s = mysql_fetch_assoc($sql_query)){
  6.     foreach($s as $k=>$v){
  7.     $insert_elsewhere_sql .= "$k = '$v', ";
  8. }
  9.  
  10. $insert_elsewhere_sql = substr($insert_elsewhere_sql, 0, -2);

This will generate a valid SQL insert statement with all of your field, value pairs set up.

Time saver or what?

More Reading:

One Comments

sinbao
October 22nd, 2009

but when I post a form data, there is a button and submit in the array , should replace it.

 

 

Leave a Reply