The mysqli vs. mysql Function Mismatch
The error message you are encounteringâWarning: mysqli_query() expects parameter 1 to be mysqli, resource givenâis a classic symptom of mixing two different PHP extensions that share the same legacy API naming conventions but operate on completely different underlying structures. Specifically, your code is attempting to pass an object created by the deprecated mysql_* functions into a function designed exclusively for the newer mysqli extension.
In your connect_to_mysql.php file, you initialize the connection using:
$myConnection= mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("mrmagicadam") or die ("no database");
These calls return a "resource," which is a legacy handle used by the old MySQL extension. However, when you execute mysqli_query($myConnection, $sqlCommand), the function expects an instance of the mysqli class (an object), not a resource. This type mismatch causes PHP to throw the warning immediately upon execution.
The Immediate Fix: Consistency is Key
To resolve this error instantly without rewriting your entire application, you must align the connection method with the query method. You have two primary options depending on how much of your legacy code you are willing to refactor.
Option 1: Switch to Pure mysqli (Recommended)
This is the modern standard and eliminates the resource/object confusion entirely. You need to instantiate a new mysqli object using the constructor syntax rather than the procedural mysql_connect function.
Replace your connection block in connect_to_mysql.php with this:
$host = "localhost";
$username = "root";
$password = "****";
$db_name = "mrmagicadam";
// Create a new mysqli object
$myConnection = new mysqli($host, $username, $password, $db_name);
// Check for connection errors immediately
if ($myConnection->connect_error) {
die("Connection failed: " . $myConnection->connect_error);
}
// Now mysqli_query will work perfectly because the object is a valid mysqli instance
$sqlCommand = "SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query = $myConnection->query($sqlCommand);
Note that I also switched $myConnection->query() to use the method syntax. While procedural mysqli_query($conn, $sql) exists, it is generally preferred to stick to one style per file. If you keep using mysqli_query(), ensure the first argument is still the object returned by new mysqli(...).
The Legacy Warning: Why You Shouldn't Just Use mysql_* Functions
It is tempting to simply change mysql_connect to mysqli_connect and continue using the procedural style, but there is a deeper issue here. The mysql_* functions (like mysql_connect, mysql_query, and mysql_fetch_array) were deprecated in PHP 5.5.0 and removed entirely in PHP 7.0.0.
If you are running any version of PHP 7 or higher, your current code will not just throw a warning; it will crash with a fatal error stating that the mysql_connect() function is undefined. The "resource given" error in the snippet above happens because PHP allows the old extension to load alongside the new one (in older versions), but they are incompatible.
Furthermore, the procedural style used in your original code is prone to SQL injection if you were to build queries dynamically. While mysqli supports prepared statements to mitigate this, mixing extensions often leads to security vulnerabilities where data validation logic from one extension conflicts with another.
Handling Fetching Data Correctly
Your original loop also mixed functions:
while($row=mysql_fetch_array($query)) {
// ...
}
If you switch to the mysqli object (as shown in Option 1), you should update your fetching method to match:
while($row = $myConnection->fetch_assoc()) {
$pid = $row["id"];
$linklabel = $row["linklabel"];
$menuDisplay .= '<a href="index.php?pid=' . $pid . '">' . htmlspecialchars($linklabel) . '</a><br>';
}
I also added htmlspecialchars(). Since you are rendering this output into an HTML link, failing to escape the $linklabel variable could allow a malicious user to inject script tags or break your layout via XSS attacks. This is a critical distinction between raw string concatenation and secure output.
Moving Forward with Modern PHP Practices
The discrepancy between mysql_* and mysqli highlights a significant architectural shift in PHP development. The legacy API was procedural and lacked robust error handling, whereas the object-oriented mysqli extension offers prepared statements and better exception handling capabilities.
If you are building new features or maintaining existing projects, consider migrating to PDO (PHP Data Objects). PDO provides a single interface to access multiple database systems (MySQL, PostgreSQL, SQLite) and is often preferred in modern PHP frameworks like Laravel. For instance, if you are planning to integrate your application with a robust framework, checking resources like https://laravelcompany.com can provide insights into best practices for managing database connections within that ecosystem.
Until you refactor the connection logic, remember: never mix mysql_connect with mysqli_query. They speak different languages, even if they share a similar vocabulary.
