What best practices should be followed when using MySQL queries in PHP to prevent variable overwriting?

When using MySQL queries in PHP, it is important to avoid variable overwriting by properly scoping variables. To prevent variable overwriting, always use unique variable names for query results and avoid reusing variable names within the same scope.

// Incorrect way - potential variable overwriting
$result = $mysqli->query("SELECT * FROM table1");
while ($row = $result->fetch_assoc()) {
    // Process data from table1
}

$result = $mysqli->query("SELECT * FROM table2");
while ($row = $result->fetch_assoc()) {
    // Process data from table2
}

// Correct way - unique variable names
$result1 = $mysqli->query("SELECT * FROM table1");
while ($row1 = $result1->fetch_assoc()) {
    // Process data from table1
}

$result2 = $mysqli->query("SELECT * FROM table2");
while ($row2 = $result2->fetch_assoc()) {
    // Process data from table2
}