What steps can be taken to troubleshoot PHP MySQL query issues when migrating to a new server?
When migrating to a new server, PHP MySQL query issues may arise due to differences in server configurations or MySQL versions. To troubleshoot these issues, ensure that the MySQL server is running, check for any syntax errors in the query, and verify that the database connection details are correct.
<?php
$servername = "new_server";
$username = "new_username";
$password = "new_password";
$dbname = "new_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Sample query
$sql = "SELECT * FROM table_name";
$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();
?>