If I have mysql table foo with fields a, b, c, d with multiple records and a being the primary key. Record 1 has the following data: a = 1, b = true, c = val1a, d = val2a.
If I want to access the data using php, I do:
$result = mysql_query("SELECT * FROM foo WHERE a = '1'");
$rec = mysql_fetch_array($result);
To change b and c, I would do:
mysql_query("INSERT INTO foo (b,c) VALUES (false,'newval') WHERE a = '1'");
Is there a way to write the whole record without listing all the fields (a,b,c,d)?
What I would like to do is write the whole $rec back to table foo:
$rec['b'] = false;
$rec['c'] = 'newval';
mysql_query("INSERT INTO foo $rec");
I know I could do a foreach and build the whole insert, but is there already a php function that does what I am tryng to accomplish?
Simple answer – no. There are a lot of things that should be considered when inserting the values, what their type is, have they been escaped or not, do they need quotes around them or not, etc. So generally all the values cannot be treated the same and just added to a string. It’s better to use an extension like MySQLi, not build the queries by concatenating stings. A simple function can do what you want with a loop as you’ve guessed, but I wouldn’t recommend that.
Also – you’ve mistaken about something:
is invalid syntax. You need to use
UPDATEtochange b and c. You also don’t need the quotes around the id, i.e.WHERE a = '1'should beWHERE a = 1, it’s an integer value.