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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.