{"id":2069,"date":"2011-11-14T14:44:03","date_gmt":"2011-11-14T13:44:03","guid":{"rendered":"http:\/\/flaven.fr\/?p=2069"},"modified":"2026-05-13T08:26:07","modified_gmt":"2026-05-13T06:26:07","slug":"php-ligne-de-commande-mysql-utiliser-des-scripts-php-en-ligne-de-commande","status":"publish","type":"post","link":"https:\/\/flaven.fr\/2011\/11\/php-ligne-de-commande-mysql-utiliser-des-scripts-php-en-ligne-de-commande\/","title":{"rendered":"PHP, Ligne de commande, MySQL &#8211; Utiliser des scripts php en ligne  de commande"},"content":{"rendered":"<p><!--  PHP, Ligne de commande, MySQL - Utiliser des scripts php en ligne  de commande --><br \/>\n<!--  PHP, Shell , MySQL, Mac, MAMP, LAMP, WAMP, script --><\/p>\n<p>Utiliser du PHP en ligne de commande peut pr\u00e9senter un int\u00e9r\u00eat si vous avez besoin d&#8217;un langage de scripting plus sophistiqu\u00e9 que le bash shell qui reste forc\u00e9ment assez limit\u00e9. Vous faites d&#8217;un certaine fa\u00e7on d&#8217;une pierre deux coups dans la mesure o\u00f9 tous les script PHP que vous d\u00e9veloppez et votre pratique du PHP sont disponibles plus seulement dans une simple page web via un navigateur.<!--more--> Le recours \u00e0 PHP permet d&#8217;\u00e9tendre consid\u00e9rablement les possibilit\u00e9s qui vous sont offertes dans des taches ardus si elles \u00e9taient \u00e0 concevoir avec \u00e0 votre disposition du bash shell uniquement.<\/p>\n<p><b>Prenons un cas concret qui consiste \u00e0 effectuer la migration de contenu d&#8217;une Base De Donn\u00e9es (BDD) de type MySQL d&#8217;un site g\u00e9r\u00e9 gr\u00e2ce \u00e0 un CMS inconnu vers la BDD d&#8217;un site sous WordPress.<\/b><\/p>\n<h4>D\u00e9terminer la localisation et la version de PHP<\/h4>\n<p>On va proc\u00e9der \u00e0 cette op\u00e9ration en local sous MAMP, qui contient en se connectant \u00e0 la fois via la console au client MySQL et en ex\u00e9cutant nos scripts PHP dans un autre onglet de la console.<\/p>\n<p>Les commandes ci-dessous permettent de d\u00e9terminer le <code>shebang<\/code> au d\u00e9but du script de sorte que cela indique au syst\u00e8me que ce fichier doit \u00eatre interpr\u00e9t\u00e9 comme du PHP.<\/p>\n<p><b>Les diff\u00e9rentes commandes n\u00e9cessaires \u00e0 d\u00e9terminer la version l&#8217;emplacement de php<\/b><\/p>\n<pre lang=\"bash\">\nwhich php\nwhereis php\ntype -a php\nphp -v\n<\/pre>\n<p><b>Voil\u00e0 donc ce qu&#8217;il faudra placer \u00e0 l&#8217;en-t\u00eate de votre script<\/b><\/p>\n<pre lang=\"bash\">\n\t#!\/usr\/bin\/php\n<\/pre>\n<h4>Lancer le script en ligne de commande<\/h4>\n<p>Ensuite, il reste \u00e0 rendre ex\u00e9cutable votre fichier PHP par la commande suivante <code>nom_du_fichier_contenant_votre_script.php<\/code>.<\/p>\n<pre lang=\"bash\">\nchmod +x \/le\/chemin\/vers\/votre\/fichier\/nom_du_fichier_contenant_votre_script.php\n<\/pre>\n<p><b>Vous n&#8217;aurez plus qu&#8217;ensuite \u00e0 lancer votre script de la mani\u00e8re suivante :<\/b><\/p>\n<pre lang=\"bash\">\n\/le\/chemin\/vers\/votre\/fichier\/nom_du_fichier_contenant_votre_script.php\n<\/pre>\n<h4>Se connecter \u00e0 une BDD sous MAMP<\/h4>\n<p>Vous pouvez vous connecter au client MySQL de MAMP \u00e0 l&#8217;aide de la commande suivante :<br \/>\n<code>\/Applications\/MAMP\/Library\/bin\/mysql -u root -p<\/code><br \/>\nPour tester votre requ\u00eate, vous pourrez passer directement votre requ\u00eate dans la client MySQL, cela est toujours utile avant de la lancer votre script en ligne de commande.<\/p>\n<p><b>Le contenu PHP du script <code>nom_du_fichier_contenant_votre_script.php<\/code><\/b><\/p>\n<pre lang=\"php\" escaped=\"true\">\n\t#!\/usr\/bin\/php\n\t&lt;?php \n\n\techo \"nnnnnn\";\n\t\techo \"--- LAUNCH ---\";\n\techo \"nnnnnn\";\n\n\t\t\t$username = \"root\";\n\t\t\t\/* Sous MAMP le pwd est root *\/\n\t\t\t$password = \"root\";\n\t\t\t\/* C'est le moyen de se connceter en mode \u00e0 la base de donn\u00e9es en mode php Cli *\/\n\t\t\t$hostname = \":\/Applications\/MAMP\/tmp\/mysql\/mysql.sock\"; \n\n\n\t\t\t\/\/connection to the database\n\t\t\t$dbhandle = mysql_connect($hostname, $username, $password)\n\t\t\t  or die(\"Unable to connect to MySQL\");\n\t\t\t\/\/ echo \"Connected to MySQL\";\n\t\t\t\t\/\/ echo \"nn\";\n\n\t\t\t\/\/select a database to work with\n\t\t\t$selected = mysql_select_db(\"db_source\",$dbhandle)\n\t\t\t  or die(\"Could not select db_source\");\n\n\t\t\t\/\/execute the SQL query and return records\n\n\t\t\t\n\t\t\t\n\t\t\t$result = mysql_query(\" SELECT * FROM posts WHERE user_id = 101 AND status = 'publish' \"); \/\/ FROM TCMES\n\t\t\t$post_author = '3'; \/\/ The id for coming for the wp_users\n\t\t\t$db_name = 'db_destination';\n\t\t\t$file = '\/le\/chemin\/vers\/votre\/fichier\/dump_sql_vers_wordpress.sql';\n\t\t\t\n\t\t\twhile ($row = mysql_fetch_array($result)) {\n\t\t\t\t\/* VALUES *\/\n\t\t\t\t$output = \"\";\n\t\t\t\t$output .= \"\n\t\t\t\tINSERT INTO \".$db_name.\".wp_posts (ID, post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type, comment_count) VALUES\";\n\t\t\t\t\/* ID *\/\n\t\t\t\t$output .=  \"('\".trim($row{'id'}).\"', \";\n\t\t\t\t\/* post_author *\/\n\t\t\t\t$output .=  \"'\".trim($post_author).\"',\";\n\t\t\t\t\/* post_date *\/\n\t\t\t\t$output .=  \"'\".trim($row{'created_at'}).\"',\";\n\t\t\t\t\/* post_date_gmt *\/\n\t\t\t\t$output .=  \"'\".trim($row{'created_at'}).\"',\";\n\t\t\t\t\/* post_content *\/\n\t\t\t\t$output .=  \"'\".addslashes(trim($row{'body'})).\"',\";\n\t\t\t\t\/* post_title *\/\n\t\t\t\t$output .=  \"'\".addslashes(trim($row{'title'})).\"',\";\n\t\t\t\t\/* post_excerpt *\/\n\t\t\t\t$output .=  \"'',\";\n\t\t\t\t\/* post_status *\/\n\t\t\t\t$output .=  \"'publish',\";\n\t\t\t\t\/* comment_status *\/\n\t\t\t\t$output .=  \"'open',\";\n\t\t\t\t\/* ping_status *\/\n\t\t\t\t$output .=  \"'open',\";\n\t\t\t\t\/* post_password *\/\n\t\t\t\t$output .=  \"'',\";\n\t\t\t\t\/* post_name (slug) *\/\n\t\t\t\t$output .=  \"'\".trim($row{'nicetitle'}).\"',\";\n\t\t\t\t\/* to_ping *\/\n\t\t\t\t$output .=  \"'',\";\n\t\t\t\t\/* pinged *\/\n\t\t\t\t$output .=  \"'',\";\n\t\t\t\t\/* post_modified *\/\n\t\t\t\t$output .=  \"'\".trim($row{'updated_at'}).\"',\";\n\t\t\t\t\/* post_modified_gmt *\/\n\t\t\t\t$output .=  \"'\".trim($row{'updated_at'}).\"',\";\n\t\t\t\t\/* post_content_filtered *\/\n\t\t\t\t$output .=  \"'',\";\n\t\t\t\t\/* post_parent *\/\n\t\t\t\t$output .=  \"'0',\"; \n\t\t\t\t\/* guid *\/\n\t\t\t\t$output .=  \"'',\";\n\t\t\t\t\/* menu_order *\/\n\t\t\t\t$output .=  \"'0',\";\n\t\t\t\t\/* post_type *\/\n\t\t\t\t$output .=  \"'post',\";\n\t\t\t\t\/* post_mime_type *\/\n\t\t\t\t$output .=  \"'',\";\n\t\t\t\t\/* comment_count *\/\n\t\t\t\t$output .=  \"'\".trim($row{'total_comments'}).\"'\";\n\t\t\t\t\/* END *\/\n\t\t\t\t$output .=  \");\";\n\t\t\t\t$output .=  \"n\";\n\n\n\t\t\t\t\/* INJECT *\/\n\t\t\t\t$handle = fopen($file, 'a'); \n\t\t\t\tfwrite($handle, $output );\n\t\t\t\tfclose($handle);\n\t\t\t\t\/* \/\/ INJECT *\/\n\t\t\t}\n\t\t\techo \"nnnnnn\";\n\t\t\techo \"--- DONE ---\";\n\t\t\techo \"nnnnnn\";\n\n\t?&gt;\n\t\n<\/pre>\n<p><b>La structure de la table MySQL &#8220;source&#8221; inclus dans la BDD <\/b><\/p>\n<pre lang=\"sql\">\n\t\n\tCREATE TABLE `posts` (\n\t  `id` int(11) NOT NULL AUTO_INCREMENT,\n\t  `created_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',\n\t  `updated_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',\n\t  `title` varchar(255) NOT NULL DEFAULT '',\n\t  `nicetitle` varchar(255) NOT NULL DEFAULT '',\n\t  `body` text NOT NULL,\n\t  `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',\n\t  `user_id` int(11) NOT NULL DEFAULT '0',\n\t  `status` varchar(11) NOT NULL DEFAULT '0',\n\t  `last_comment_date` datetime DEFAULT NULL,\n\t  `total_comments` int(11) DEFAULT '0',\n\t  `blocked` tinyint(1) DEFAULT '0',\n\t  `unique_id` varchar(255) DEFAULT NULL,\n\t  `imported_feed_id` int(11) DEFAULT NULL,\n\t  PRIMARY KEY (`id`),\n\t  UNIQUE KEY `unique_id` (`unique_id`),\n\t  UNIQUE KEY `unique_id_2` (`unique_id`),\n\t  KEY `nicetitle` (`nicetitle`),\n\t  KEY `user_id` (`user_id`),\n\t  KEY `date` (`date`),\n\t  KEY `status` (`status`),\n\t  FULLTEXT KEY `searching` (`title`,`body`)\n\t) ENGINE=MyISAM  DEFAULT CHARSET=latin1;\n\t\n<\/pre>\n<h2>En savoir plus<\/h2>\n<ul>\n<li>Getting command line access to PHP and MySQL running MAMP on OSX<br \/>\n\t\t<a href=\"http:\/\/www.metaltoad.com\/blog\/getting-command-line-access-php-and-mysql-running-mamp-osx\" target=\"_blank\">http:\/\/www.metaltoad.com\/blog\/getting-command-line-access-php-and-mysql-running-mamp-osx<\/a><\/li>\n<li>Running PHP CLI shell scripts<br \/>\n\t\t<a href=\"http:\/\/blog.johan-mares.be\/ict\/php\/running-php-shell-scripts\/\" target=\"_blank\">http:\/\/blog.johan-mares.be\/ict\/php\/running-php-shell-scripts\/<\/a><\/li>\n<li>PHP on the Command Line<br \/>\n\t<a href=\"http:\/\/www.sitepoint.com\/php-command-line-1\/\" target=\"_blank\">http:\/\/www.sitepoint.com\/php-command-line-1\/<\/a><\/li>\n<li>UPDATE Syntax dans MySQL<br \/>\n\t<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/update.html\" target=\"_blank\">http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/update.html<\/a><\/li>\n<li>How to select from an update target in MySQL<br \/>\n\t<a href=\"http:\/\/www.xaprb.com\/blog\/2006\/06\/23\/how-to-select-from-an-update-target-in-mysql\/\" target=\"_blank\">http:\/\/www.xaprb.com\/blog\/2006\/06\/23\/how-to-select-from-an-update-target-in-mysql\/<\/a><\/li>\n<li>A propos de <code>COUNT(*)<\/code> dans MySQL<br \/>\n<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/fr\/counting-rows.html\" target=\"_blank\">http:\/\/dev.mysql.com\/doc\/refman\/5.0\/fr\/counting-rows.html<\/a><\/li>\n<li>Programmer ses backups Dotclear gr\u00e2ce \u00e0 un script bash et CRON<br \/>\n<a href=\"http:\/\/www.prendreuncafe.com\/blog\/post\/2006\/01\/22\/382-programmer-ses-backups-dotclear-grace-a-un-script-bash-et-cron\" target=\"_blank\">http:\/\/www.prendreuncafe.com\/blog\/post\/2006\/01\/22\/382-programmer-ses-backups-dotclear-grace-a-un-script-bash-et-cron<\/a><\/li>\n<li>How to connect to MySQL database using PHP<br \/>\n\t<a href=\"http:\/\/www.webcheatsheet.com\/PHP\/connect_mysql_database.php\" target=\"_blank\">http:\/\/www.webcheatsheet.com\/PHP\/connect_mysql_database.php<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Utiliser du PHP en ligne de commande peut pr\u00e9senter un int\u00e9r\u00eat si vous avez besoin d&#8217;un langage de scripting plus sophistiqu\u00e9 que le bash shell&hellip; <\/p>\n<p class=\"text-center\"><a href=\"https:\/\/flaven.fr\/2011\/11\/php-ligne-de-commande-mysql-utiliser-des-scripts-php-en-ligne-de-commande\/\" class=\"more-link\">Continue reading &rarr; <span class=\"screen-reader-text\">PHP, Ligne de commande, MySQL &#8211; Utiliser des scripts php en ligne  de commande<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":9412,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"bf_ai_meta_description":"PHP, ligne de commande, MySQL: utilisez des scripts PHP pour automatiser des t\u00e2ches complexes. Acc\u00e9dez \u00e0 MySQL et migrez des bases de donn\u00e9es.","bf_ai_og_title":"PHP, ligne de commande, MySQL","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[3440,3444,3447,3448,3449,3435,3452],"tags":[579,415,3455,2210,416,2211,20,392,417,418],"class_list":["post-2069","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-infrastructure","category-programming-databases","category-technology-trends","category-tools-productivity","category-tutorials-how-to","category-web-development","category-wordpress-cms","tag-apache","tag-lamp","tag-linux","tag-mac","tag-mamp","tag-mysql","tag-php","tag-script","tag-shell","tag-wamp"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3Vuhl-xn","jetpack_featured_media_url":"https:\/\/flaven.fr\/wp-content\/uploads\/2011\/11\/php_cli_b_g_bf.jpg","_links":{"self":[{"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/posts\/2069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/comments?post=2069"}],"version-history":[{"count":1,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/posts\/2069\/revisions"}],"predecessor-version":[{"id":13208,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/posts\/2069\/revisions\/13208"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/media\/9412"}],"wp:attachment":[{"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/media?parent=2069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/categories?post=2069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flaven.fr\/happy-api\/wp\/v2\/tags?post=2069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}