Solved

use a php function and not exclusively mysql for my learning

Posted on 2014-02-17
4
357 Views
Last Modified: 2014-03-06
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.
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39865544
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

0
 

Author Comment

by:rgb192
ID: 39865549
but how would this work with a select and update

making sure to update the correct row.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39865569
You would SELECT the column with the AUTO_INCREMENT id (Primary key) and the column you wanted to add information to.  With each row of the results set you would use the pend() function to change the column you wanted to add information to.  Then you would UPDATE the table, SET column you wanted with new information WHERE the id matched, LIMIT 1.
0
 

Author Closing Comment

by:rgb192
ID: 39911432
okay this is an example of a mysql connection and php function combined.

Thanks.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question