0
DELETE FROM Table WHERE TableR = '1';
INSERT INTO Table (TableR, Seet, Keyword) VALUES ('1', '1', 'test1');
INSERT INTO Table (TableR, Seet, Keyword) VALUES ('1', '2', 'test2');
INSERT INTO Table (TableR, Seet, Keyword) VALUES ('1', '3', 'test3');

For connect we use code:

$mysqli = new mysqli($this->MysqlHost,$this->MysqlUser,$this->MysqlPassword,$this->MysqlDatabase);
$result = $mysqli->query("SET NAMES utf8");
$result = $mysqli->query("set character_set_client='utf8'");
$result = $mysqli->query("set collation_connection='utf8_general_ci'");
$result = $mysqli->query($query);

Now we need use several query for run all query.

But i would be like run only one query, ie:

$query = "
        DELETE FROM Table WHERE TableR = '1';
        INSERT INTO Table (TableR, Seet, Keyword) VALUES ('1', '1', 'test1');
        INSERT INTO Table (TableR, Seet, Keyword) VALUES ('1', '2', 'test2');
        INSERT INTO Table (TableR, Seet, Keyword) VALUES ('1', '3', 'test3');
    ";

Tell me please how to run several queries in a single query ?

2
  • @PeterMmm we use mysqli.. Commented Jul 1, 2014 at 13:07
  • @Kermani why nee use left joing anf other ?? Commented Jul 1, 2014 at 13:08

3 Answers 3

1
$result = $mysqli->multi_query($query);

Enjoy!

Sign up to request clarification or add additional context in comments.

Comments

1

With multiquery?

You can check the following documentation:

http://php.net/manual/en/mysqli.multi-query.php

If you are trying to insert multiple rows, you can check:

Batch insertion of data to MySQL database using php

(look for LOAD DATA INFILE solution...)

Comments

0

I hope this help you: http://www.php.net/manual/en/mysqli.quickstart.multiple-statement.php

If you use it, be careful about security.

Example #1 Multiple Statements

<?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());
?>

output:

array(1) {
  [0]=>
  array(1) {
    ["_num"]=>
    string(1) "0"
  }
}
array(1) {
  [0]=>
  array(1) {
    ["_num"]=>
    string(1) "1"
  }
}

1 Comment

Please summarize the content of the URL in your answer, so if the link goes offline the answer will still be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.