martes, 18 de marzo de 2025

select function

   <?php

function select($query){


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);  // 1 enable reporting


$mysqli = new mysqli("localhost", "my_user", "my_password", "world");  // 2 connect to the db


if ($mysqli->connect_error) {                                     // 3 check connection error

    /* Use your preferred error logging method here */

    error_log('Connection error: ' . $mysqli->connect_error);

    exit();

}


//$query = "SELECT Name, CountryCode FROM City ORDER BY ID LIMIT 3";  // 4 corrected query


// Execute the query and check for errors

$result = $mysqli->query($query); 

if (!$result) {  // Check if the query failed

    printf("Error message: %s\n", $mysqli->error);

    exit();

}


$mysqli->close();  // 5 Close the connection as soon as it's no longer needed


// Fetch results as an associative array

return $rows = $result->fetch_all(MYSQLI_ASSOC);  // 6 print result as assoc array

//foreach ($rows as $row) {

  //  printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);

//}

print_r(select($query =$argv[1]\));


?>


 php select.php "select * from users;" 

 Array

(

    [0] => Array

        (

            [userId] => 60

            [phoneNumber] => 12127960900

            [accessCode] => 593161

            [pin] => 1234

            [nameURL] => https://rdasteriskrecordings.s3.amazonaws.com/12127960900.wav

            [lastCalled] => 2025-03-06

            [userLang] => en

            [userAuthMode] => cid

        )


    [1] => Array

        (

            [userId] => 61

            [phoneNumber] => 100001

            [accessCode] => 406366

            [pin] => 1234

            [nameURL] => https://rdasteriskrecordings.s3.amazonaws.com/2100001.wav

            [lastCalled] => 2025-03-06

            [userLang] => en

            [userAuthMode] => cid

        )


)


No hay comentarios:

Publicar un comentario