October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Fix “mysql_num_rows() Expects Parameter 1 to Be Resource, Boolean Given”

The warning means a failed query returned false before mysql_num_rows() could count rows. Trace the connection and generated SQL, then migrate legacy mysql_* code to MySQLi or PDO_MySQL.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This warning means mysql_num_rows() received false instead of a query-result resource. The row-count call is where the problem becomes visible; for a SELECT, the earlier mysql_query() call returns false when it fails. Diagnose that query and its connection before trying to count rows.

What the warning means

The legacy mysql_num_rows() function expects a result resource. If it receives a boolean, it cannot count rows. In this situation the boolean is usually false, returned because the preceding query failed. PHP’s documentation for mysql_query() describes the return behavior; the mysql_num_rows() manual documents the row-count function.

As an Amazon Associate I earn from qualifying purchases.

That is different from a successful query that returns zero rows: a valid result set with no matches is still a result, and can be counted. Do not treat a failed query as an empty result.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Trace the failure before line 54

  1. Check the connection and selected database. Confirm that connection setup succeeded and that the intended database was selected before issuing the query. The SitePoint example does not check those steps, so it cannot establish whether either failed in a particular installation. Stop the operation or report an application-safe error if setup fails.
  2. Keep the SQL and result in separate variables. For example, use $sql for the statement text and $result for the return value of mysql_query($sql). Reusing a variable named $query for both makes it harder to tell whether it contains SQL or the query outcome.
  3. Inspect the exact SQL sent to the database. Log or display it only in a controlled development environment, then verify the table and column names, syntax, and values. Check the database error at the query line rather than waiting until mysql_num_rows() reports the bad value.
  4. Check how the search terms are assembled. In the code discussed in the SitePoint Forums thread, community participant John_Betong points out that $i = 0 is placed inside a foreach loop. That resets $i on every iteration, so it becomes 1 each time and can prevent the intended SQL-building branch from changing. Move initialization before the loop and inspect the resulting OR terms. Also verify that $q has the expected value. The thread does not include the database’s actual error, so this is a likely flaw in that snippet, not a proven explanation for every instance of the warning.
  5. Handle failure before counting or fetching. In legacy code, test the query result for failure and stop downstream row-count or fetch operations if it failed. Record detailed database diagnostics in a controlled log or development environment; do not expose raw database errors to site visitors.

Why replacing the row-count call is not the fix

Changing the line number, suppressing the warning, or swapping out mysql_num_rows() does not repair a failed query. The cause could be connection setup, database selection, malformed SQL, missing or misspelled tables or columns, or invalid input construction. The available SitePoint example leaves the source of $q and the exact database error unknown, so identify the cause from the actual query and error in your own application.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use a supported database API in current PHP

The original PHP mysql_* extension was deprecated in PHP 5.5.0 and removed in PHP 7.0.0. PHP directs developers to MySQLi or PDO_MySQL instead; see the mysql_query() documentation for its compatibility note. Do not try to restore the removed extension as a fix for a current PHP installation.

MySQLi and PDO_MySQL are both supported alternatives. Select the one that fits the application’s existing database layer and the team’s chosen API style; the cited documentation does not establish a universal winner. Keep connection, query, and result handling within the same API.

MySQLi

With mysqli::query(), a successful result-producing query returns a mysqli_result; failure returns false unless error reporting is configured to raise an exception. Check or catch the failure according to the application’s configuration before using the result. If a query contains variable input, PHP recommends parameterized prepared statements; see mysqli::prepare().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

PDO_MySQL

With PDO, query failure behavior depends on the configured error mode: a call may return false or throw an exception. Check PDO::query() and handle errors consistently with that setting. For variable values, prepare the statement and execute it with bound values rather than concatenating input into SQL.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.