Peter.P Peter.P - 6 months ago 9x
PHP Question

How to run Multiple querys at the same time?

I am stuck at a point where I want to run multiple queries but i cannot do it.

I have this code now I have there my 4 queries I want to run at the same time but I don't know how to do it, I read a lot of other questions on the forum already. Some say I can separate them of ; signs or use the mysqli_multi_query .. but I don't know it anymore.

Here a printscreen of my mysql db:

And connection code:

define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'mysql_enquete');

$db_link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Verbindingsfout");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());

if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("", $row[0]);

and the code where i have dropdown menus for the bands

Nummer 1<br/>
<?php echo '<select name="bands1">';
echo '<option>Kies een band</option>';

$sql="SELECT * FROM bands ORDER BY band";
$result = mysqli_query($db_link, $sql);

if (!$result){
die ("Database connection failed!");

while($row = mysqli_fetch_assoc($result)){

echo '<option value="' . $row['BandID'] . '">' . $row['band'] . '</option>';


echo '</select>';

I hope somebody can tell me how i can run all those querys in the picture

$sql = "
UPDATE bands
SET punten = punten + 10
WHERE bandID = '".$bands1."';

UPDATE bands
SET punten = punten + 6
WHERE bandID = '".$bands2."';

UPDATE bands
SET punten = punten + 3
WHERE bandID = '".$bands3."';

INSERT INTO users (ID, email)
VALUES ('NULL','$email')



There's no need to run all the queries at once. Just perform each query separately. However, you can combine the 3 UDPATE queries into a single query using a CASE expression.

So the code should look like this (I'm also showing how to use a prepared statement instead of substituting variables).

$stmt = mysqli_prepare($db_link, "
    UPDATE bands
    SET punten = 
        CASE bandID
            WHEN ? THEN punten + 10
            WHEN ? THEN punten + 6
            WHEN ? THEN punten + 3
    WHERE bandID IN (?, ?, ?)");
mysqli_stmt_bind_param($stmt, "iiiiii", $bands1, $bands2, $bands3, $bands1, $bands2, $bands3);

$stmt2 = mysqli_prepare($db_link, "INSERT INTO users (ID, email) VALUES (NULL, ?)");
mysqli_stmt_bind_param($stmt2, "s", $email);