Hi All,

I am new to PHP, I have tried to look for similar error but could not find
anwers that helped me. I am getting the following error:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result
resource in C:\ ... register.php on line 109

Here is my code snippets:
1.) my connection to the database is per page. the first three lines of code
in my script is :

<?php
session_start();
mysql_connect("localhost", "USER", "PASS") or die(mysql_error());
mysql_select_db("DATABASE") or die(mysql_error());

which means that I do not use persistant connections

2.) the code that creates the error is this:

$checkSQL = "SELECT COUNT(`EmailAddress`) AS `CountOfRows` FROM
`printerclient` WHERE `EmailAddress` = '$email'";
$sqlresult = mysql_query($checkSQL);
$sqlrow = mysql_fetch_row($sqlresult); <<-- THIS IS LINE 109 -->>
if ($sqlrow['CountOfRows'] == 0) {
$IsUnique = true;
} else {
$registerMessage = "This email address is already registered";
}

What I have tried to find out what is wrong:
1.) If I change the following line

$sqlresult = mysql_query($checkSQL);
to this
$sqlresult = mysql_query($checkSQL) or die(mysql_error());

then I get the following message
Query was empty
when I echo the $checkSQL and copy it from the screen and run it in
phpMyAdmin it run OK and returns CountOfRows = 2

The SQL statement generated looks like this

SELECT COUNT(`EmailAddress`) AS `CountOfRows` FROM `printerclient` WHERE
`EmailAddress` = 'test@test.com'

I do not know what is wrong, can somebody help?

Thanks.
Peter.

Dani AI

Generated

Short answer: that warning means the value passed into mysql_fetch_row was not a valid result resource — most commonly because mysql_query returned false (an error) or the query string was empty. correctly highlighted fetch types, and ’s workaround shows the symptom changing when the fetch call changes, but the underlying issue is almost always a failed or malformed query or an unset/overwritten variable.

Practical diagnostic steps used by experienced developers:

  • Check the exact string and type you send to the server (use var_dump($checkSQL) just before the query; whitespace/empty strings are common culprits).
  • Always test the query result before fetching: if the query returned false, log mysql_error() and do not call any fetch function.
  • Confirm which fetch form you need: numeric, associative, or both — mismatch only changes how you read the row, it does not make the result resource valid.

Example pattern for safe handling:

$result = mysql_query($query);
if ($result === false) {
  error_log('MySQL error: ' . mysql_error());
  // don't call mysql_fetch_*, handle the error instead
} else {
  $count = (int) mysql_result($result, 0);  // scalar COUNT retrieval
}

Notes and cautions: do not display raw DB errors to users; escape or bind input to avoid SQL injection; the old mysql_* extension is deprecated — migrate to mysqli or PDO with prepared statements for security and forward-compatibility. For modern code a PDO prepared query and fetchColumn() is the simplest, safe way to get a COUNT.

Recommended Answers

All 3 Replies

This is strange. But one thing.

$sqlrow = mysql_fetch_row($sqlresult); <<-- THIS IS LINE 109 -->>
if ($sqlrow == 0) {

You have used mysql_fetch_row. It returns rows as . So, you can't give, $sqlrow. You can use instead. I am not sure if this will help, but remove ` from your query !

THANKS, THANKS, THANKS ...
using mysql_fetch_array($sqlresult, MYSQL_NUM) did the trick.

It still is a mystery why the original code did not work however using mysql_fetch_array did work.

Removing of ' did not work, whether they are there or not the use of mysql_fetch_row was the problem, if any body knows why please point me to documentation that may help me understand. The PHP docs does not give me enough info to understand why?

Thank you.

Peter.

I am not really sure why you got "Query was empty" message. :-O

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.