Question

EDITED: Working code below!

//* opens a connection to a MySQL server */
$mysqli = new mysqli($host, $username, $password, $database);

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}


$query = "INSERT INTO `test` (test1, test2) VALUES('testing','testing2222');";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing3333','testing44444');";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing5555','testing66666');";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing77777','testing888888');";
$query .= "INSERT INTO `test` (test1, test2) VALUES('testing99999','testing101010');";


if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                //printf("%s\n", $row[0]);
            }
            $result->free();
        }
     /* print divider */
    //if ($mysqli->more_results()) {
    //  printf("-----------------\n");
    //}
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();           

BAD CODE below:

I've tried this every way possible and I can't seem to get it to work. I notice one variation would work only if I had one $query to input. If it ever had more than one, it would fail.

//* opens a connection to a MySQL server */
$mysqli = mysql_connect($host,$username,$password, $database);



$query  = "INSERT INTO `test` (test1, test2) VALUES('testing','testing2222')";
$query  .= "INSERT INTO `test` (test1, test2) VALUES('testing3333','testing44444')";


if($mysqli->multi_query($query))
 { 
    do
    {

        if($result=$mysqli->store_result())
        {
            while($row=$result->fetch_row())
            {
                printf("%s<br/>",$row[0]);
            }
            $result->free();
        }

        if($mysqli->more_results())
        {
            print("-------------------------------<br/>");
        }
        else
        {
            echo '<br/>';
        }
    }while($mysqli->more_results() && $mysqli->next_result());
 }

It makes a connection, I just trimmed some of the code to make it easier to follow. Table exist, etc... Tried the main example also, word for word, and it doesn't seem to work. Where are I going wrong?

Était-ce utile?

La solution

You have to put ; on the end of each statement. Also always check for errors $mysqli->error.

Now your query will look exactly like this

INSERT INTO `test` (test1, test2) VALUES('testing','testing2222')INSERT INTO `test` (test1, test2) VALUES('testing3333','testing44444')

which isn't correct.

Autres conseils

You need a semi-colon at the end of each query. MySQL is waiting for the end of the statement. See the following from MySQL documentation:

A command need not be given all on a single line, so lengthy commands that require several lines are not a problem. mysql determines where your statement ends by looking for the terminating semicolon, not by looking for the end of the input line. (In other words, mysql accepts free-format input: it collects input lines but does not execute them until it sees the semicolon.)

Change this:

$query  = "INSERT INTO `test` (test1, test2) VALUES('testing','testing2222')";
$query  .= "INSERT INTO `test` (test1, test2) VALUES('testing3333','testing44444')";

to this:

$query  = "INSERT INTO `test` (test1, test2) VALUES('testing','testing2222');";
$query  .= "INSERT INTO `test` (test1, test2) VALUES('testing3333','testing44444');";
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top