Jump to content

Static not sticking


scottybwoy

Recommended Posts

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 ;)
Link to comment
https://forums.phpfreaks.com/topic/33875-static-not-sticking/
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/33875-static-not-sticking/#findComment-158973
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.