Solved

use a php function and not exclusively mysql for my learning

Posted on 2014-02-17
4
351 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
  • 2
  • 2
4 Comments
 
LVL 109

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 109

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

821 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