lunes, 10 de marzo de 2025

mysqi step by step

 <?php


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

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

foreach ($rows as $row) {

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

}


?>


sábado, 8 de marzo de 2025