Skittalz Posted August 6, 2008 Share Posted August 6, 2008 When using a MySQL UPDATE, is it possible to use a variable as the field name as well as a variable for the field value? My field name is in a loop and I am trying to update my MySQL column values... Heres my code @mysql_query("INSERT INTO statistics_past(`startdate`) VALUES ('".date("Y-m-d H:i:s")."') "); $sql = mysql_query("SELECT * FROM statistics_past"); $Mr = mysql_num_rows($sql); $result = mysql_query("SELECT * FROM statistics"); while($row = mysql_fetch_array($result)) { $id = (int) $row['id']; echo $Mr. " - ".$id; @mysql_query("UPDATE statistics_past SET '".$id."' = '".$row['w_views']."' WHERE id = '" . mysql_real_escape_string($Mr). "'"); } @mysql_query("UPDATE statistics_past SET `enddate` = ('".date("Y-m-d H:i:s")."') WHERE id = '" . mysql_real_escape_string($Mr - 1). "'"); Any help would be appreciated ... I've been looking at this code for to long ... Thanks! Steve Quote Link to comment Share on other sites More sharing options...
fenway Posted August 6, 2008 Share Posted August 6, 2008 MySQL doesn't know about php variables. Which query are we talking about? Does one not work? You're absorbing errors. Quote Link to comment Share on other sites More sharing options...
Skittalz Posted August 7, 2008 Author Share Posted August 7, 2008 @mysql_query("UPDATE statistics_past SET '".$id."' = '".$row['w_views']."' WHERE id = '" . mysql_real_escape_string($Mr). "'"); This MySQL doesn't work Quote Link to comment Share on other sites More sharing options...
Skittalz Posted August 7, 2008 Author Share Posted August 7, 2008 Nevermind I figured it out The problem was I was using single quotes (') around the field name when I shoulda been using a grave accent (`) .... good ole php n mysql 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.