Browsing articles tagged with " concat"
adding data to a sql column value
i came across an interesting challenge the other day that i found a rather simple solution to.
i was trying to update a SQL table to add information to the end of a column value. i simply wanted to add some text to the end of what was already stored there. i will be honest, i dont consider myself to be a mysql expert by any means and my first approach was to bring the data into php, add the extra text and then update the table.
wrong way:
$new_data = "some value"; $sql = "SELECT * FROM table WHERE id = 1"; $result = mysql_result($sql); $row = mysql_fetch_assoc($result); mysql_free_result($result); $new_col = $row['column'] . $new_data; $sql = "UPDATE table SET column = $new_col WHERE id = 1"; $result = mysql_query($sql)
with the use of mysql’s CONCAT function this can be simplified into much more efficient coding.
right way:
$new_data = "some value"; $sql = "UPDATE table SET column = CONCAT(column, $new_data) WHERE id = 1"; $result = mysql_query($sql);
thats it! 8 lines of code reduced to 2 lines. gotta love taking advantage of built in functionality!
Tags
AJAX
arrays
ascii art
big-o-notation
concat
data structures
email validation
event handler
gd library
geolocation
getElementById
goal thermometer
hash table
JavaScript/Ajax
json
md5
multithreading
MySQL
obfuscation
object oriented programming
open source
PHP
recursion
regular expression
select statement
star wars
variables
web apps
XML
xml dom
XMLHttpRequest




