martes, 11 de febrero de 2025

multiquery

<?php


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);


// Create a connection to the database

$mysqli = new mysqli("localhost", "root", "", "asterisk");


// Check connection

if ($mysqli->connect_error) {

    die("Connection failed: " . $mysqli->connect_error);

}


// Multiple SELECT queries to execute

$query = "SELECT CURDATE() AS DATE;

          SELECT NOW() AS NOW;

          SELECT YEAR(NOW()) AS YEAR";


// Execute multiple queries

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

    do {

        // Store the result set of each query

        if ($result = $mysqli->store_result()) {

            while ($row = $result->fetch_assoc()) {

                // Correct way to access values from the associative array

                // Here we check if the keys exist and print the results accordingly

                if (isset($row['DATE'])) {

                    echo "DATE: " . $row['DATE'] . " | ";

                }

                if (isset($row['NOW'])) {

                    echo "NOW: " . $row['NOW'] . " | ";

                }

                if (isset($row['YEAR'])) {

                    echo "YEAR: " . $row['YEAR'];

                }

                echo "<br>";

            }

            $result->free(); // Free result set

        }


        // Print divider if there are more results

        if ($mysqli->more_results()) {

            echo "-----------------\n";

        }

    } while ($mysqli->next_result());

}


$mysqli->close(); // Close the database connection


?>


No hay comentarios:

Publicar un comentario