Forum Moderators: mack

Message Too Old, No Replies

Mysql query help

         

hswaseer

12:53 pm on Aug 7, 2004 (gmt 0)

10+ Year Member



Hi

I have 500 records in my database and I want to add a line at the start of my each record.

What will be the query for this.

Thanks

HS

MichaelBluejay

7:59 pm on Aug 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not sure I understand what you're asking. Are you saying that you want to add (prepend) text to the beginning of a certain field for each record in the database? If so, and if the database doesn't contain identical records, then I don't think you can do it with a simple MySQL command. I think you'd have to write a script with Perl or PHP instead. For example:

#!/usr/bin/perl

use DBI;
$SQLhost = 'mysqldomain.com';
$SQLname = 'databasename';
$SQLuser = 'username';
$SQLpass = 'password';

$DB = DBI->connect("DBI:mysql:$SQLname:$SQLhost", $SQLuser, $SQLpass);

$DB->do("SELECT fieldname FROM tablename");
$cursor = $DB->prepare($SQL);
$cursor->execute;

while ( @data = $cursor->fetchrow ) {
$DB->do("UPDATE tablename SET fieldname = 'prepended text $data[0]' where fieldname = '$data[0]'");
}
$cursor->finish;
$DB->disconnect;

---------------
This script requires that every record has a unique value for fieldname. Also, I haven't tested it to double-check it.

Hope this helps.