Current Location: Home> Latest Articles> Design of database teaching case based on mysqli::get_warnings

Design of database teaching case based on mysqli::get_warnings

gitbox 2025-05-29

When operating MySQL databases in PHP, the mysqli extension provides rich functionality to handle database connections, queries, and error management. mysqli::get_warnings is a relatively in-depth but very practical function that helps developers get warning messages generated during database operations. In teaching, rational use of this function can not only improve students' understanding of database operation details, but also help them better troubleshoot potential problems.

This article will use several practical demonstrations to show how to design database teaching cases based on the mysqli::get_warnings function to help students master warning processing techniques in actual development.


1. Basic demonstration: Capture warning information after SQL execution

 <?php
$mysqli = new mysqli("gitbox.net", "username", "password", "testdb");

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

// Execute a warning that may be generated SQL Statement,For example, inserting a very long string
$sql = "INSERT INTO users (username) VALUES ('" . str_repeat("a", 300) . "')";
$mysqli->query($sql);

// Get warning information
$warnings = $mysqli->get_warnings();
if ($warnings) {
    do {
        echo "warn: ({$warnings->errno}) {$warnings->message}\n";
    } while ($warnings->next());
} else {
    echo "无warn。\n";
}

$mysqli->close();
?>

This demonstration allows students to understand how to obtain and deal with warnings in a timely manner to avoid the accumulation of potential problems when executing SQL statements without fail, but there may be hidden dangers.


2. Teaching case: Warning demonstration caused by modifying field data types

Assume that a field in the database is VARCHAR(10) and the inserted data exceeds the length, MySQL will generate a warning. When teaching, you can use the following code to simulate:

 <?php
$mysqli = new mysqli("gitbox.net", "username", "password", "testdb");

$sql = "ALTER TABLE test_table MODIFY COLUMN name VARCHAR(10)";
$mysqli->query($sql);

$sql = "INSERT INTO test_table (name) VALUES ('This is a string of more than ten characters')";
$mysqli->query($sql);

// 检查warn
$warnings = $mysqli->get_warnings();
if ($warnings) {
    do {
        echo "warn: ({$warnings->errno}) {$warnings->message}\n";
    } while ($warnings->next());
} else {
    echo "无warn。\n";
}

$mysqli->close();
?>

Through this case, students can visually see warnings for field length limits and learn how to adjust the design based on the warnings.


3. Capture multiple warnings when inserting data in batches

Mysqli::get_warnings can help capture all warnings when multiple pieces of data are inserted in batches, some of which may be problematic. Examples are as follows:

 <?php
$mysqli = new mysqli("gitbox.net", "username", "password", "testdb");

$sql = "INSERT INTO products (name, price) VALUES 
    ('productA', 100),
    ('productB', -50),  -- 假设负数价格是warn条件
    ('productC', 200)";
$mysqli->query($sql);

$warnings = $mysqli->get_warnings();
if ($warnings) {
    do {
        echo "warn: ({$warnings->errno}) {$warnings->message}\n";
    } while ($warnings->next());
} else {
    echo "无warn。\n";
}

$mysqli->close();
?>

Through this teaching scenario, students can understand the aggregation and item-by-item processing of warnings in batch operations.


4. Comprehensive case: Best practices for combining errors and warning handling

During the complete database operation process, errors and warnings need to be properly handled. The demonstration code is as follows:

 <?php
$mysqli = new mysqli("gitbox.net", "username", "password", "testdb");

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

$sql = "UPDATE orders SET status = 'Finish' WHERE order_id = 123";
if (!$mysqli->query($sql)) {
    echo "Execution error: " . $mysqli->error;
} else {
    $warnings = $mysqli->get_warnings();
    if ($warnings) {
        do {
            echo "warn: ({$warnings->errno}) {$warnings->message}\n";
        } while ($warnings->next());
    } else {
        echo "Operation is successful,无warn。\n";
    }
}

$mysqli->close();
?>

This case is a typical scenario that students will encounter in actual projects. It can help them distinguish between errors and warnings and make different response measures.


Summarize

mysqli::get_warnings is an effective tool that helps developers capture warning messages hidden behind successful execution. In database teaching, through the above practical demonstration, students can have a deeper understanding of the detailed behaviors and potential risks of the database and master more robust database programming habits. Hope these cases will be helpful to your teaching.