martes, 11 de febrero de 2025

Simple SQL Query

<?php


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);


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


// Check for connection errors

if ($mysqli->connect_error) {

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

}


$query = "SELECT now() as now";


// Execute the query and check for errors

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


// Check if the query was successful

if (!$result) {

    die("Query failed: " . $mysqli->error);

}


/* Get the number of fields in the result set */

$field_cnt = $result->field_count;


/* Get the number of rows in the result set */

$row_cnt = $result->num_rows;


/* Fetch associative array */

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

    echo "Total of values: Fields = $field_cnt, Rows = $row_cnt, Current Time = $row['now']<br>";

}


$mysqli->close();

?>


----------------

<?php


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);


$mysqli = new mysqli("localhost", "my_user", "my_password", "world");


// Check for connection errors

if ($mysqli->connect_error) {

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

}


$result = $mysqli->query("SELECT Name, CountryCode FROM City ORDER BY ID LIMIT 3");


// Check if query was successful

if (!$result) {

    die("Query failed: " . $mysqli->error);

}


// Fetch all rows as an associative array

$rows = $result->fetch_all(MYSQLI_ASSOC);


foreach ($rows as $row) {

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

}


$mysqli->close();

?>


No hay comentarios:

Publicar un comentario