A simple IF condition.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a simple IF condition to check whether an entered town name for
a search actually exists in the database.

The form search produces $townsearch

The check should return "Cannot Find The Town" if $townsearch does not
correspond to a recognised townname in the database.

The database is correct, and the SQL query OK.

So why can I type anything in and it accepts it??

(Day 4 of learning PHP)


<?php $townsearch = $_GET['town']; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<input type="text" name="town" size="20" value="<?php echo $townsearch;
?>" />&nbsp;
<input type="submit" value="GO" />

$check = @mysql_query ("SELECT townname FROM site01_towns WHERE
if (!$check)  
echo '<p>Cannot Find The Town</p>';



Re: A simple IF condition.

*** Alec wrote/escribió (2 Sep 2005 05:46:29 -0700):
Quoted text here. Click to load it

Let's check mysql_query() manual page:

"Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query() returns
a resource identifier or FALSE if the query was not executed correctly.
[...] A non-FALSE return value means that the query was legal and could be
executed by the server. It does not indicate anything about the number of
rows affected or returned. It is perfectly possible for a query to succeed
but affect no rows or return no rows."

Your if condition will only be FALSE if you have a syntax error in your SQL

-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group

Re: A simple IF condition.


if (mysql_num_rows($check) == 0)

a try instead.

Re: A simple IF condition.


If (mysql_num_rows($check) ==0)

Works nicely thank you very much....

Am learning as I am doing, but eventually....

Re: A simple IF condition.

Is this the entire code?
If so, you don't have a mysql connection yet (afaik).

And *please* escape your search object:

Greetings Frizzle.

Site Timeline