Form Tools
Best way to prepare variable(s) for UPDATE query? (mysql/php) - Printable Version

+- Form Tools (https://forums.formtools.org)
+-- Forum: Form Tools (https://forums.formtools.org/forumdisplay.php?fid=1)
+--- Forum: General Discussion (https://forums.formtools.org/forumdisplay.php?fid=5)
+--- Thread: Best way to prepare variable(s) for UPDATE query? (mysql/php) (/showthread.php?tid=1262)



Best way to prepare variable(s) for UPDATE query? (mysql/php) - crunchers - May 5th, 2011

Go easy (still learning).

Just wondering what the best approach would be for dynamically updating a record in mysql using PHP without using a loop when defining the row data as an array (key=column name, value=value).

I'd like to prepare the key and value from the array as a string and run the query once instead of iterating through the array using a loop for each key/value.

e.g.

PHP Code:
$data = array('col1' => 'one',
                     
'col2' => 'two',
                     
'col3' => 'three'
                     
);

$str = ????

$query = ("UPDATE table_name SET ".$str." WHERE some_column=some_value"

I can figure out how to do it using a foreach loop; and I can easily prepare a variable when inserting multiple records using php implode function. I just can't figure out what the best approach would be for updating the table in the same fashion.


RE: Best way to prepare variable(s) for UPDATE query? (mysql/php) - crunchers - May 5th, 2011

Nevermind. I apply the loop to the array instead of the query; what a moron! Just goes to show, you can't legislate for stupidity.

++A

:q

PHP Code:
$data = array('col1' => 'one',
                     
'col2' => 'two',
                     
'col3' => 'three'
                     
);

foreach (
$data as $field => $value) {
  
$list[] = $field.'='."'".mysql_real_escape_string($value)."'";
}
$fields implode(', '$list);
    
$query "UPDATE table_name SET ".$fields." WHERE some_column=some_value"



RE: Best way to prepare variable(s) for UPDATE query? (mysql/php) - Vernorar - Nov 16th, 2020

Here is the best way for the variable to update query cod explain that how we can use it to update that variable. It is very helpful in programming language resume spice reviews explains me all features and share your feedback.


RE: Best way to prepare variable(s) for UPDATE query? (mysql/php) - lufafujahi - Nov 16th, 2020

This is one oldest design of England pounds. So, why you are still living in older times and not moving to further levels of
epoxy countertops diy  approach? You should share comparison, if you are still willing to talk about them.