/    Sign up×
Community /Pin to ProfileBookmark

code to determine query returned no record

Hi

I’m trying to use the expression:
if (mysql_num_rows($qry) > 0)
to test whether a record exists with certain criteria. I get the error “mysql_num_rows(): supplied argument is not a valid MySQL result resource” – I assume because $qry doesn’t return anything. What’s the right way to do this test?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 23.2006 — [code=php]
$result = mysql_query($query);
if($result) // query did not fail outright
{
if(mysql_num_rows($result))
{
// we got at least 1 result row
}
else
{
// no matches found in DB
}
}
else
{
// query failed (such as due to SQL syntax error)
// might want to echo a mysql_error() here for debugging purposes
}
[/code]
Copy linkTweet thisAlerts:
@strBeanauthorJun 23.2006 — Thanks Nog, that's where I was headed.

In my tests, if

(!mysql_num_rows($result)) // 0 rows

then

(!$result) // query failed

so in my tests, the second test is unnecessary.

Anyway, much obliged.
Copy linkTweet thisAlerts:
@NogDogJun 23.2006 — Thanks Nog, that's where I was headed.

In my tests, if

(!mysql_num_rows($result)) // 0 rows

then

(!$result) // query failed

so in my tests, the second test is unnecessary.

Anyway, much obliged.[/QUOTE]

Not quite: the second test is telling you the query failed to process, not that it was successful but no rows were returned. In other words, if the mysql_num_rows() returns 0, that's telling you that the query was properly formed and processed, but no data was found in the DB that matched it; whereas if the result of your mysql_query() is false, then your SQL was incorrect, or the database connection was down, or some other error not related to whether or not there was matching data in the database.
Copy linkTweet thisAlerts:
@strBeanauthorJun 23.2006 — Hmmm. I can't get mysql_num_rows() to return zero. It appears to be returning False if no data is returned by the query. I must be overlooking something.

Right now I have

if ((!$qry) or (!mysql_num_rows($qry)))

and it seems to be doing what I want.

However, if I don't understand what the code is doing, then I'm open to unexpected errors, so I still need to look closely at this. Thanks for your help so far...
Copy linkTweet thisAlerts:
@bokehJun 23.2006 — It appears to be returning False if no data is returned by the query.[/QUOTE]It will only return false if you feed it an invalid resource and in that instance it will raise an [I]E_WARNING[/I] level error.
×

Success!

Help @strBean spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...