PHP Generated SQL

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

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

Imagine this:

$sql_query = mysql_query("select * from table");

$insert_elsewhere_sql = "INSERT INTO other_table SET ";

while($s = mysql_fetch_assoc($sql_query)){
	foreach($s as $k=>$v){
	$insert_elsewhere_sql .= "$k = '$v', ";
}

$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?