On Thu, Feb 26, 2009 at 06:07:02PM -0500, PJ wrote:

> Is there a shorter way of determining if a field contains text?
> This works but I wonder if I can K.I.S.S. it? I really only need to know
> if there is or is not a field containing the text specified. I don't
> need to see it.
> <?php
>   // Request the text
>   $text = "Joe of Egypt";
>   $sql = "SELECT title FROM book WHERE title LIKE '$text'";
>   $result = mysql_query($sql);
>   if (!$result) {
>     echo("<P>Error performing query: " .
>          mysql_error() . "</P>");
>     exit();
>   }
>   // Display the text
>   while ( $row = mysql_fetch_array($result) ) {
>     echo("<P>" . $row["title"] . "</P>");
>   }
>   if ($row["title"] == "")
>       echo ("Empty!")
> ?>

If you just want to see whether the passed text is in the title field,
and not display it, then no, there's no simpler way. However, I would
suggest you surround the $text with percent signs (%) to allow mysql to
check if this text is embedded within any of the titles. (Er, that's the
way you do it in PostgreSQL; I assume MySQL uses the same mechanism.)

Also, I don't know that mysql_query() returns *false* on no rows. I
generally check that with mysql_num_rows(). Again, I don't know MySQL
that well. PostgreSQL only returns *false* if there's a problem with the
query. If the query yields nothing, then you have to check with


Paul M. Foster

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to