Home > Archive > PHP Language > May 2006 > Problem with else statement
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem with else statement
|
|
| Colin Copland 2006-05-23, 6:58 pm |
| X-No-Archive: Yes
Hi,
I am trying to write my first php script to query an SQL db. The db is
simple and has only two rows term and description. The search page is
so that the user can search for a technical term and get a small
description.
So far I have added a few records to the db and have simple script that
works fine. The only thing I would like to add is if the searched for
value is not in the term row then a error message should be returned. I
have tried to add in a else statement but have had no luck so far.
Search page (http://www.fullyconnected.co.uk/search.shtml)
===========
<form method="post" action="search.php">
<blockquote>
<p>Enter term to search for
<input type=text name='enteredterm' size=20 maxlength=20>
<input type=submit class="loginButton">
<input name="Reset" type="reset" class="loginButton"
value="Reset" />
</p>
</blockquote>
</form>
Search script (http://www.fullyconnected.co.uk/search.php)
=============
<?php
$con = mysql_connect("localhost","********","********");
if (!$con)
{die('Unable to connect to database: ' . mysql_error());
}
mysql_select_db("fullycon_search", $con);
$result = mysql_query("SELECT * FROM `Search` WHERE `term` =
'$enteredterm'");
while($row = mysql_fetch_array($result))
{
echo $row['term'];
echo "<br />";
echo $row['description'];
}
?>
db
==
Simple databse called fullycon_search with a table called search
containing two fields, term and description.
I would like to add in a else statement so that if the term searched for
is not in the db it would display a simple error message. Having tried
many times I cannot seem to get the syntax correct for the else
statement. Can anyone help? If anyone needs to try a sample search a
couple of terms in the table are cisco, linux, windows, stow......
It's all still very much a work in progress at present!
Thanks,
Colin
| |
| David Haynes 2006-05-23, 6:58 pm |
| Colin Copland wrote:
> X-No-Archive: Yes
>
> Hi,
>
> I am trying to write my first php script to query an SQL db. The db is
> simple and has only two rows term and description. The search page is
> so that the user can search for a technical term and get a small
> description.
>
> So far I have added a few records to the db and have simple script that
> works fine. The only thing I would like to add is if the searched for
> value is not in the term row then a error message should be returned. I
> have tried to add in a else statement but have had no luck so far.
>
> Search page (http://www.fullyconnected.co.uk/search.shtml)
> ===========
> <form method="post" action="search.php">
> <blockquote>
> <p>Enter term to search for
> <input type=text name='enteredterm' size=20 maxlength=20>
>
> <input type=submit class="loginButton">
> <input name="Reset" type="reset" class="loginButton"
> value="Reset" />
> </p>
> </blockquote>
> </form>
>
> Search script (http://www.fullyconnected.co.uk/search.php)
> =============
> <?php
> $con = mysql_connect("localhost","********","********");
>
> if (!$con)
> {die('Unable to connect to database: ' . mysql_error());
> }
>
> mysql_select_db("fullycon_search", $con);
>
> $result = mysql_query("SELECT * FROM `Search` WHERE `term` =
> '$enteredterm'");
>
> while($row = mysql_fetch_array($result))
> {
> echo $row['term'];
> echo "<br />";
> echo $row['description'];
> }
> ?>
> db
> ==
> Simple databse called fullycon_search with a table called search
> containing two fields, term and description.
>
> I would like to add in a else statement so that if the term searched for
> is not in the db it would display a simple error message. Having tried
> many times I cannot seem to get the syntax correct for the else
> statement. Can anyone help? If anyone needs to try a sample search a
> couple of terms in the table are cisco, linux, windows, stow......
>
> It's all still very much a work in progress at present!
>
> Thanks,
> Colin
The mysql API allows you to ask how many rows were returned in the
result. In your case, returning zero rows means that the term was not
found in the table by the query.
So:
mysql_select_db("fullycon_search", $con);
if( mysql_num_rows($result) == 0 ) {
// no match code
} else {
while($row = mysql_fetch_array($result)) {
echo $row['term'];
echo "<br />";
echo $row['description'];
}
mysql_free_result($result);
-david-
| |
| Colin Copland 2006-05-24, 3:58 am |
| X-No-Archive: Yes
David Haynes wrote:
> The mysql API allows you to ask how many rows were returned in the
> result. In your case, returning zero rows means that the term was not
> found in the table by the query.
Many thanks, that work perfectly now. Have a simple echo command
executed if the search term if not in the table.
Cheers,
Colin
|
|
|
|
|