Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

use a php function and not exclusively mysql for my learning

using mysqli, or pdo

I would like a php function that prepends 'begin' and postpends 'end' to an existing varchar field

table.column1 int primary key autoincrement
table.column2 varchar
table.column3 varchar


example output
5454  begin5454end
656    begin656end
4        begin4end
A        beginAend


while this can be done with only mysql

UPDATE table SET column3 = CONCAT("begin",column2,"end")

I would like to use a php function so I can learn php functions.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Untested code, but probably OK  in theory:

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// A STARTING STRING
$old = '5454';

// IN OPEN CODE
$new = 'begin' . $old . 'end'; 

// WITH A FUNCTION
function pend($str, $alpha = 'begin', $omega = 'end')
{
    return $alpha . $str . $omega;
}

// VIA A FUNCTION CALL
echo pend($old);

Open in new window

Avatar of rgb192

ASKER

but how would this work with a select and update

making sure to update the correct row.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

okay this is an example of a mysql connection and php function combined.

Thanks.