scottybwoy Posted January 12, 2007 Share Posted January 12, 2007 Hi I have a function that is tring to make a string out of an array for a sql statement, but my static $var clears each time it's passed through my foreach statement, can anyone see why?[code]<?php function updateCust($data_array, $update) { static $colvals; $custId = $data_array["custId"]; $this->appCon(); $sql = "SELECT * FROM customers WHERE custId = '" . $custId . "'"; $row = mssql_fetch_assoc(mssql_query($sql)); array_pop($update); $diff = array_diff_assoc($update, $row); print_r($diff); $colvals = ""; foreach ($diff as $k => $v) { $colvals = $k . " = '" . $v . "', "; } $sql = "UPDATE customers SET " . $colvals . " WHERE custId = " . $custId; echo $sql; }?>[/code]I print out the var data and it shows all the details I want, however the sql statement just shows the last key and value pair. Thanks for taking a peek ;) Quote Link to comment https://forums.phpfreaks.com/topic/33875-static-not-sticking/ Share on other sites More sharing options...
redbullmarky Posted January 12, 2007 Share Posted January 12, 2007 you need an extra dot to make sure that the string gets appended, not rewritten:[code]$colvals.= $k . " = '" . $v . "', ";[/code](note the dot that is directly after $colvals)hope that helpscheers Quote Link to comment https://forums.phpfreaks.com/topic/33875-static-not-sticking/#findComment-158971 Share on other sites More sharing options...
redbullmarky Posted January 12, 2007 Share Posted January 12, 2007 actually, you'll have a problem still, as the last digit of $colvals will be a comma which will cause the query to throw a wobbly. try this instead:[code] $colvals = array(); foreach ($diff as $k => $v) { $colvals[] = $k . " = '" . $v; } $colvals = implode(', ', $colvals);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33875-static-not-sticking/#findComment-158973 Share on other sites More sharing options...
scottybwoy Posted January 12, 2007 Author Share Posted January 12, 2007 LOL thanks how stupid of me. Yeah, I knew about the comma, was gonna sort that after. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/33875-static-not-sticking/#findComment-158975 Share on other sites More sharing options...
scottybwoy Posted January 12, 2007 Author Share Posted January 12, 2007 PS still not sure how to mark as solved am i really that blind Quote Link to comment https://forums.phpfreaks.com/topic/33875-static-not-sticking/#findComment-158977 Share on other sites More sharing options...
redbullmarky Posted January 12, 2007 Share Posted January 12, 2007 no worries.PS the solved mod was removed when the forum was upgraded but should be back soon ;) Quote Link to comment https://forums.phpfreaks.com/topic/33875-static-not-sticking/#findComment-158981 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.