How can I test my PHP MySQL injection example?

How can I test my PHP MySQL injection example?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your SQL Injection Test Failed and How to Actually Verify It

You are attempting to verify a SQL injection vulnerability by submitting anything' -- as the username and leaving the password field empty. The fact that this query returns no results is not surprising, and it does not mean your code is safe or that your test method is flawed in principle. The issue lies in how you are interpreting the output of a deprecated extension and why a simple string concatenation often fails to trigger a successful bypass in this specific context.

Your PHP script relies on the mysql_* extension, which was deprecated in PHP 5.5 and removed entirely in PHP 7.0. This extension does not support prepared statements, making it inherently vulnerable to SQL injection if user input is concatenated directly into queries. However, the success of an injection attack depends entirely on the database server's configuration and the specific syntax required to terminate the current query and inject a new one.

In your code, the query string looks like this:

select * from zend_adminlist where user_name = 'anything' --' and password = ''

When you append -- to the username input, you intend to comment out the rest of the query. The standard SQL syntax for a single-line comment is two hyphens followed by whitespace. If your MySQL server is configured to strictly require this whitespace (which is common in production environments), the injection will fail because the parser sees --' without a space before it, treating it as part of the string literal rather than a comment marker.

Furthermore, even if the syntax were correct, you are checking the result using mysql_num_rows(). If the query executes successfully but returns zero rows because no user exists in the database with that name, your script outputs 0. If the injection works and you bypass the authentication logic to access another table or escalate privileges, the behavior changes. However, if the injected syntax causes a SQL syntax error, the mysql_query() function will return false, and mysql_num_rows() might behave unexpectedly depending on how the result is handled before the check.

To properly test for injection in a local development environment where you control the database configuration, you must ensure that the comment syntax does not require a trailing space. You can verify this by checking your MySQL version and running a test query directly via the command line. If you are using MySQL 8.0 or higher, the behavior regarding comments has been standardized, but legacy configurations might differ.

The most reliable way to confirm an injection exists is not just by guessing strings, but by observing how the application handles SQL syntax errors versus successful queries. If your application returns a specific error message for malformed SQL, you can inject syntax that forces a different error or bypasses authentication entirely. For example, changing -- to # or /* might succeed if one works and the other fails due to configuration quirks.

The fundamental takeaway is that relying on a single test vector like anything' -- is insufficient without understanding the underlying database dialect and configuration. If you are building a new application, do not use the mysql_* functions. Instead, migrate to PDO or MySQLi with prepared statements. Prepared statements separate the SQL logic from the data, rendering injection attacks impossible regardless of what users input.

If you are working on legacy code that cannot immediately be migrated, you must manually audit every query that accepts user input. Replace direct string concatenation with parameterized queries. This is the only robust defense against SQL injection. For modern PHP development, consider adopting the Laravel framework, which enforces these security best practices by default and provides built-in tools to handle database interactions safely.

Finally, remember that testing for vulnerabilities requires a deeper understanding of the SQL dialect being used. What works on one server might fail on another due to comment syntax variations or strict mode settings. Always validate your assumptions against the actual runtime environment rather than assuming standard behavior applies universally.

Our network