I have the following code:
$pdo = new PDO('mysql:host=localhost:3306;dbname=database', 'user', 'password');
$statement = $pdo->prepare("UPDATE channelbro_rss SET links = ? WHERE channel = ?");
$statement->execute(array($links_new, $channel));
but how do I set $links_new
to the current value of links
from the database, plus a new value?
For example:
The value of links
in the database is test
. Now first of all I want to echo this value and then update the value of links
to the current value (test
) plus another variable (separated by commas). E.g.: Update links
to test, new
. And if I do this whole thing again, it will update it to test, new, hello
(The new value is always $new
).
It is simpler to add the value you wish to amend the old value by like this.
the concat()
function will as its name suggests concatenate the new string onto the existing contents of the column.
$pdo = new PDO('mysql:host=localhost:3306;dbname=web190_db8', 'web190_8', 'password');
// If you want to see the value of links before you amend it
// as your requested, then you will have to get it as a seperate
// SELECT query
$stmt = $pdo->prepare("SELECT links FROM channelbro_rss WHERE channel = ?");
$stmt->execute( array($channel) );
$old_links = $stmt->fetchColumn(0);
echo $old_links;
$statement = $pdo->prepare("UPDATE channelbro_rss SET links = concat(links, ?) WHERE channel = ?");
$concat_text = ', new';
$statement->execute(array($concat_text, $channel));
// If you want to see the value of links AFTER you amend it
// then you will have to get it as a seperate SELECT query also
$stmt = $pdo->prepare("SELECT links FROM channelbro_rss WHERE channel = ?");
$stmt->execute( array($channel) );
$old_links = $stmt->fetchColumn(0);
echo $old_links;