What are the implications of not specifying the database before each table in a SQL query in PHP?

When not specifying the database before each table in a SQL query in PHP, the query may not execute correctly if the tables are spread across multiple databases. To solve this issue, you can specify the database name before each table in the query to ensure that the query is executed against the correct database.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// Specify database before each table in the query
$sql = "SELECT * FROM database_name.table_name WHERE column_name = 'value'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
  }
} else {
  echo "0 results";
}

$conn->close();
?>