Question

How run two or more SQL queries together in the same line command?

Hello!

I want run multiple queries (sql) in the same line command to Update image URLs in my database of WordPress.

How I run that command connecting the comands of updates?

For example:

UPDATE wp_posts SET post_content = replace(post_content, ‘http://oldurl.com/image/image1.jpg’, http://newurl.com/image/image1.jpg’); UPDATE wp_posts SET post_content = replace(post_content, ‘http://oldurl.com/image/image2.jpg’, http://newurl.com/image/image2.jpg’); UPDATE wp_posts SET post_content = replace(post_content, ‘http://oldurl.com/image/image3.jpg’, http://newurl.com/image/image3.jpg’);

I am using nginx - Ubuntu 14.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Because you mentioned WordPress, I will provide a PHP-specific answer:

<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT)")) {
    echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

$sql = "SELECT COUNT(*) AS _num FROM test; ";
$sql.= "INSERT INTO test(id) VALUES (1); ";
$sql.= "SELECT COUNT(*) AS _num FROM test; ";

if (!$mysqli->multi_query($sql)) {
    echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

do {
    if ($res = $mysqli->store_result()) {
        var_dump($res->fetch_all(MYSQLI_ASSOC));
        $res->free();
    }
} while ($mysqli->more_results() && $mysqli->next_result());
?>

Example of PHP’s mysqli-multi-query()

See: http://php.net/manual/en/mysqli.quickstart.multiple-statement.php http://php.net/manual/en/mysqli.multi-query.php

It’s best to put these in a shell script:

#!/bin/sh
echo "This program will update blahblahblah..."
echo "After starting this program, enter your database password for root"
DATABASENAME=wordpress

mysql -u root -p <<-ENDMARKER

 use ${DATABASENAME};
 UPDATE wp_posts SET post_content = replace(post_content, 'http://oldurl.com/image/image1.jpg',  http://newurl.com/image/image1.jpg'); 
 UPDATE wp_posts SET post_content = replace(postcontent, 'http://oldurl.com/image/image2.jpg', http://newurl.com/image/image2.jpg');
 UPDATE wp_posts SET post_content = replace(post_content, 'http://oldurl.com/image/image3.jpg', http://newurl.com/image/image3.jpg');
 commit;

ENDMARKER

Make the script file executable after saving. The script method is better than using the pipe method so you can debug the script in stages, or later expand on it when you have more junk you need to cleanup.