[PHP] Re: verify text in field

2009-02-26 Thread Shawn McKenzie
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(PError 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!)
 
 ?
 
// Request the text
  $text = Joe of Egypt;
  $sql = SELECT title FROM book WHERE title LIKE '%$text%';
  $result = mysql_query($sql);
  if (!$result) {
die(PError performing query:  . mysql_error() . /P);
  }
  if (mysql_num_rows($result)  0) {
// you found text
  }

-- 
Thanks!
-Shawn
http://www.spidean.com

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



[PHP] Re: verify text in field

2009-02-26 Thread Shawn McKenzie
Shawn McKenzie wrote:
 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(PError 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!)

 ?

 // Request the text
   $text = Joe of Egypt;
   $sql = SELECT title FROM book WHERE title LIKE '%$text%';
   $result = mysql_query($sql);
   if (!$result) {
 die(PError performing query:  . mysql_error() . /P);
   }
   if (mysql_num_rows($result)  0) {
   // you found text
   }
 
Not relevant to your question, but you should be able to shorten it up
more like this:

--instead of this:

$result = mysql_query($sql);
   if (!$result) {
 die(PError performing query:  . mysql_error() . /P);
   }

--try this:

$result = mysql_query($sql) or die(PError performing query:  .
mysql_error() . /P);

-- 
Thanks!
-Shawn
http://www.spidean.com

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



[PHP] Re: verify text in field

2009-02-26 Thread David Robley
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(PError 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!)
 
 ?
 

In addition to other answers, don't forget to escape the string you are
passing to the query with mysql_real_escape_string(), otherwise your query
will have problems with some characters e.g. apostrophe.


Cheers
-- 
David Robley

I refuse to make an agenda, Tom said listlessly.
Today is Pungenday, the 58th day of Chaos in the YOLD 3175. 


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