Jump to content

Need Help Stringing Some Php Commands Together


FUNKAM35

Recommended Posts

Hi, please could someone help me

 

I need to string several about 30 in total commands together in total

 

An example of the commands are:

UPDATE `property` SET `description` = replace(description, "é", "e")

UPDATE `property` SET `description` = replace(description, "á", "a")
UPDATE `property` SET `description` = replace(description, "St.", "St ")

UPDATE `property` SET `description` = replace(description, "Ü", "u")

UPDATE `property` SET `description` = replace(description, "È", "e")
UPDATE `property` SET `description` = replace(description, "ü", "u")

UPDATE `property` SET `description` = replace(description, "ü", "u")
UPDATE `property` SET `description` = replace(description, "ß", "ss")
UPDATE `property` SET `description` = replace(description, "Ö", "o")

 

Now I have to run each one separately and manually

 

I would like to be able to join them all together and run them all in one file and then I could set up a cron job for one file

 

Last time I tried I unwittingly deleted all the descriptions, nightmare!

 

Please can someone help me?

 

Thanks

You may be surprised to learn that none of what you posted was PHP.

 

This PHP script will run the queries as long as you establish the proper database connection:

 

mysql_connect(/*proper values here*/);
$statements = array(
'UPDATE `property` SET `description` = replace(description, "é", "e")',
'UPDATE `property` SET `description` = replace(description, "á", "a")',
'UPDATE `property` SET `description` = replace(description, "St.", "St ")',
'UPDATE `property` SET `description` = replace(description, "Ü", "u")',
'UPDATE `property` SET `description` = replace(description, "È", "e")',
'UPDATE `property` SET `description` = replace(description, "ü", "u")',
'UPDATE `property` SET `description` = replace(description, "ü", "u")',
'UPDATE `property` SET `description` = replace(description, "ß", "ss")',
'UPDATE `property` SET `description` = replace(description, "Ö", "o")');
foreach ( $statements as $statement ) {
 mysql_query($statement);
}

You can also do this through bash, or pipe a file full of statements to mysql right from the cron interface.

thanks for the reply, I have been running these staetments individually through mysql datatbase but now need to join them

 

You don't need to run separated updates (and in a loop)... only ONE update is enough if you use a CASE... example

 

 

'UPDATE `property`

SET `description` = ( CASE

WHEN `description` = "é" THEN "e"

WHEN `description` = "á" THEN "a"

....

....

ELSE `description`

END

)

That case won't work. Running them all one at a time is going to be slightly slower but not so much that you'd care. My script will work, though The Little Guy provided my "other" solution, right in the cron line.

The queries are designed to replace "special" foreign characters with their latin equivalent in a field called "description." It's assumed these are actual descriptions, longer than one character. The case given will only replace descriptions that are one character long and ignore the rest. That's why he was using the REPLACE function with no WHERE.

a modified version that should cover the right scenario pointed out by Dan. granted it is a micro-optimization

 

UPDATE `property`
SET `description` = ( CASE
WHEN instr(`description`, "é") > 0 THEN replace(`description`, "é", "e")
WHEN instr(`description`, "á") > 0 THEN replace(`description`, "á", "a")
....
....
ELSE `description`
END
)

  • 2 weeks later...

hi, I have used

<?php
include_once("variables.php");
$link_id = mysql_connect("$db_host","$db_user","$db_password");
if (mysql_select_db("$db_database", $link_id));
$statements = array
('UPDATE `property` SET `town` = replace(town, "i­", "i")',
'UPDATE `property` SET `town` = replace(town, "í", "i")',
'UPDATE `property` SET `town` = replace(town, "ú", "u")');
foreach ( $statements as $statement ) {  mysql_query($statement);}
?>

 

And it doesnt work any ideas whats wrong

thanks

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><META content="IE=5.0000" http-equiv="X-UA-Compatible">
<META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD>
<BODY></BODY></HTML>

 

Thanks, I added error reporting but no errors showed

 

For some strange reason today it dis replace & with and

I figured it was something to do with the encoding that it wouldnt change ß to ss

 

I added the above headers and it worked, strange or what!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.