mrherman Posted July 5, 2008 Share Posted July 5, 2008 Greetings! How might I go about this...? ??? I want to generate a list of field names into a string variable, and then I want to update the MySQL table by assigning all of these fields to a NULL value. (I've tried literally dozens of combinations, searched the documentation for examples, and followed up with Google, but I have not been successful.) Something like... $fields = "f1,f2,f3......f50" ; $sql = "UPDATE $mytable SET $fields = NULL" ; $results = mysql_query($sql) ; Thanks for any pointers on this! Quote Link to comment Share on other sites More sharing options...
Barand Posted July 5, 2008 Share Posted July 5, 2008 Have you ever considered data normalization? http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html try <?php $fields = "f1,f2,f3,f50" ; $farray = explode(',', $fields); $sql = "UPDATE $mytable SET " ; foreach ($farray as $f) { $sql .= "\n$f = NULL"; } echo '<pre>', $sql, '</pre>'; // inspect the query $results = mysql_query($sql) ; ?> Quote Link to comment Share on other sites More sharing options...
mrherman Posted July 6, 2008 Author Share Posted July 6, 2008 BINGO!! By George, Sir, you have it! Thank ya, thank ya, thank ya!! I never would have achieved it! And, oh yes, a little normalization would be in line...The table actually has 400+ fields now and I bet it will end up with 500 or so. Not exactly a textbook model. (Well, perhaps the "bad" example.) Your help is much appreciated. Dave Quote Link to comment Share on other sites More sharing options...
Barand Posted July 6, 2008 Share Posted July 6, 2008 BINGO!! By George, Sir, you have it! Oops! Not quite. I forgot the commas <?php $fields = "f1,f2,f3,f50" ; $farray = explode(',', $fields); $sql = "UPDATE $mytable SET \n" ; foreach ($farray as $k=>$f) { $farray[$k] = "$f = NULL"; } $sql .= join(",\n", $farray); echo '<pre>', $sql, '</pre>'; // inspect the query $results = mysql_query($sql) ; ?> Quote Link to comment Share on other sites More sharing options...
mrherman Posted July 6, 2008 Author Share Posted July 6, 2008 No problem...I just stuck those in...It was the least I could do..Ha The main thing was the code. Lots of help! Dave Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.