Solved

use a php function and not exclusively mysql for my learning

Posted on 2014-02-17
4
341 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
but how would this work with a select and update

making sure to update the correct row.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
okay this is an example of a mysql connection and php function combined.

Thanks.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 look for a specific file type in a local or remote server directory using PHP.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now