I'm writing a php script that communicates with a MySQL database and formats the results as a string that can be parsed by an Objective-C Framework I'm building.
Currently I'm trying to get multiple statements/results to work, but I'm having some trouble here.
I'm sending 3 statements as a multi-statement:
- SELECT * FROM table(returns a resultset)
- SELECT field FROM table(returns a resultset)
- INSERT INTO table VALUES value(should return a duplicate error)
The do while loop gets called twice, once for each one of the first 2 statements, however it doesn't get called a third time for the INSERT statement.
if ($mysql->more_results()) {
    echo("MULTIRESULT|*|".mysqli_errno($mysql)."|*|".mysqli_error($mysql)."|*|");
    $i4 = 0;
    do {
        if ($i4 != 0) {
            echo("|&|");
        }
        $result = $mysql->store_result();
        if ($result) {
            echo("RESULTSET|%|");
            $i1 = 0;
            while ($field = $result->fetch_field()) {
                if ($i1 != 0) {
                    echo("|+|");
                } echo($field->name."|@|".$field->orgname."|@|".$field->table."|@|".$field->orgtable."|@|".$field->db."|@|".$field->catalog."|@|".$field->def."|@|".$field->length."|@|".$field->maxlength."|@|".$field->flags."|@|".$field->decimals."|@|".$field->charsetnr."|@|".$field->type);
                $i1++;
            }
            echo("|~|");
            $i2 = 0;
            while ($row = $result->fetch_row()) {
                if ($i2 != 0) {
                    echo("|+|");
                }
                $fieldcount = $mysql->field_count;
                for ($i3 = 0; $i3 < $fieldcount; $i3++) {
                    if ($i3 != 0) {
                        echo("|@|");
                    } echo($row[$i3]);
                }
                $i2++;
            }
        } else if ($mysql->field_count == 0) {
            echo("AFFECTEDROWS|%|".$mysql->affected_rows);
        } else {
            echo("UNSUCCESSFUL|%|".mysqli_errno($mysql)."|%|".mysqli_error($mysql));
        } $result->close();
        $i4++;
    } while ($mysql->next_result());
}
For the piece of code that handles a single statement this works fine. There is no result, the mysqli_field_count is not 0, so it echoes the line starting with UNSUCCESSFUL.
$result = $mysql->store_result();
if ($result) {
    echo("RESULTSET|*|".mysqli_errno($mysql)."|*|".mysqli_error($mysql)."|*|");
    $i1 = 0;
    while ($field = $result->fetch_field()) {
        if ($i1 != 0) {
            echo("|+|");
        } echo($field->name."|@|".$field->orgname."|@|".$field->table."|@|".$field->orgtable."|@|".$field->db."|@|".$field->catalog."|@|".$field->def."|@|".$field->length."|@|".$field->maxlength."|@|".$field->flags."|@|".$field->decimals."|@|".$field->charsetnr."|@|".$field->type);
        $i1++;
    }
    echo("|~|");
    $i2 = 0;
    while ($row = $result->fetch_row()) {
        if ($i2 != 0) {
            echo("|+|");
        }
        $fieldcount = $mysql->field_count;
        for ($i3 = 0; $i3 < $fieldcount; $i3++) {
            if ($i3 != 0) {
                echo("|@|");
            } echo($row[$i3]);
        }
        $i2++;
    }
} elseif ($mysql->field_count == 0) {
    echo("AFFECTEDROWS|*|".mysqli_errno($mysql)."|*|".mysqli_error($mysql)."|*|".$mysql->affected_rows);
} else {
    echo("UNSUCCESSFUL|*|".mysqli_errno($mysql)."|*|".mysqli_error($mysql));
} $result->close();
I would say it is because mysqli_next_result only works in case there is a resultset, but when I do an INSERT statement when there's no duplicate yet, the do while loop does get called for that statement.
I'm so in the dark about this issue that I don't even know how to formulate my question, but I guess the question here is pretty obvious.
 
     
    