FUNKAM35 Posted October 2, 2012 Share Posted October 2, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/ Share on other sites More sharing options...
ManiacDan Posted October 2, 2012 Share Posted October 2, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382274 Share on other sites More sharing options...
FUNKAM35 Posted October 2, 2012 Author Share Posted October 2, 2012 thanks for the reply, I have been running these staetments individually through mysql datatbase but now need to join them Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382277 Share on other sites More sharing options...
The Little Guy Posted October 2, 2012 Share Posted October 2, 2012 (edited) Just put it in a sql file and run from the commandline: mysql -h localhost -u myUserName -pMyPassword myDatabase < mySQLFile.sql Edited October 2, 2012 by The Little Guy Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382280 Share on other sites More sharing options...
mikosiko Posted October 2, 2012 Share Posted October 2, 2012 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382283 Share on other sites More sharing options...
FUNKAM35 Posted October 2, 2012 Author Share Posted October 2, 2012 ok thanks will try this just terrified as last time it updated the description to nothing and all descriptions on 30,000 items were gone Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382285 Share on other sites More sharing options...
mikosiko Posted October 2, 2012 Share Posted October 2, 2012 ok thanks will try this just terrified as last time it updated the description to nothing and all descriptions on 30,000 items were gone just don't forget the ELSE part Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382290 Share on other sites More sharing options...
ManiacDan Posted October 2, 2012 Share Posted October 2, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382291 Share on other sites More sharing options...
mikosiko Posted October 2, 2012 Share Posted October 2, 2012 That case won't work. why not?... did you tested?.. I did... it does work... maybe you are seeing something that I'm not... enlight us Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382293 Share on other sites More sharing options...
ManiacDan Posted October 2, 2012 Share Posted October 2, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382300 Share on other sites More sharing options...
mikosiko Posted October 2, 2012 Share Posted October 2, 2012 (edited) absolutely valid point Dan, I didn't see the query from that angle... if that was the OP objectives as it seems, you are absolutely correct, my code suggestion doesn't work in that scenario; thank you for correct my Uh-oh moment. Edited October 2, 2012 by mikosiko Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382304 Share on other sites More sharing options...
mikosiko Posted October 2, 2012 Share Posted October 2, 2012 (edited) 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 ) Edited October 2, 2012 by mikosiko Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382307 Share on other sites More sharing options...
ManiacDan Posted October 2, 2012 Share Posted October 2, 2012 Will MySQL continue to evaluate CASE statements? What if the description contains 8 different special characters? Will all 8 of the replacements evaluate? Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382321 Share on other sites More sharing options...
mikosiko Posted October 2, 2012 Share Posted October 2, 2012 you are right again... I should have stayed at home today Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382332 Share on other sites More sharing options...
jazzman1 Posted October 2, 2012 Share Posted October 2, 2012 Well, what about with differen charset and collation like UTF ? Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382336 Share on other sites More sharing options...
fenway Posted October 3, 2012 Share Posted October 3, 2012 You know, MySQL has collation conversions specifically to address this issue. Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382426 Share on other sites More sharing options...
FUNKAM35 Posted October 3, 2012 Author Share Posted October 3, 2012 Can you please tell me more about these mysql collation conversions? thanks Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382450 Share on other sites More sharing options...
FUNKAM35 Posted October 3, 2012 Author Share Posted October 3, 2012 confused.com so which of the examples should I use? Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382452 Share on other sites More sharing options...
ManiacDan Posted October 3, 2012 Share Posted October 3, 2012 The very first example I gave you will work. Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1382486 Share on other sites More sharing options...
FUNKAM35 Posted October 14, 2012 Author Share Posted October 14, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1385118 Share on other sites More sharing options...
ManiacDan Posted October 14, 2012 Share Posted October 14, 2012 Turn on error reporting, check the errors, echo mysql_error() Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1385203 Share on other sites More sharing options...
FUNKAM35 Posted October 15, 2012 Author Share Posted October 15, 2012 <!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! Quote Link to comment https://forums.phpfreaks.com/topic/269006-need-help-stringing-some-php-commands-together/#findComment-1385265 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.