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 ?

Apostle
  • 482
  • 2
  • 7
  • 23

3 Answers3

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...)

Community
  • 1
  • 1
lpg
  • 4,897
  • 1
  • 16
  • 16
1
$result = $mysqli->multi_query($query);

Enjoy!

Leo Loki
  • 2,457
  • 6
  • 24
  • 29
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"
  }
}
jlgsoftware
  • 172
  • 8
  • 1
    Please summarize the content of the URL in your answer, so if the link goes offline the answer will still be useful. – SysDragon Jul 01 '14 at 13:31