Re: [PHP] verify text in field
ad...@buskirkgraphics.com wrote: > Try > $text = "Joe of Egypt"; > $sql = "SELECT title FROM book WHERE title LIKE '$text'"; > $result = mysql_query($sql); > If(mysql_num_rows($result) >= '1') > { > while ( $row = mysql_fetch_array($sql) ) { > echo("" . $row["title"] . ""); > } > } > > My Ideas my not make some people happy with my design but I use it and it > works very well > > > > -Original Message- > From: PJ [mailto:af.gour...@videotron.ca] > Sent: Thursday, February 26, 2009 5:07 PM > To: php-general@lists.php.net > Subject: [PHP] verify text in field > > 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. > > // Request the text > $text = "Joe of Egypt"; > $sql = "SELECT title FROM book WHERE title LIKE '$text'"; > $result = mysql_query($sql); > if (!$result) { > echo("Error performing query: " . > mysql_error() . ""); > exit(); > } > > // Display the text > while ( $row = mysql_fetch_array($result) ) { > echo("" . $row["title"] . ""); > } > if ($row["title"] == "") > echo ("Empty!") > > ?> > I think I was not precise in my question. I meant that I do need to be able to use an if condition to determine which instruction will be executed next: e.g. if the $return is ""(empty or 0) then I INSERT data somewhere; if the $return is a string or text or 1, then I either do a different insert or I abort the operation and ask to restart it with different criteria. I was just wondering if there was conditional operation I could use that would replace this: = if (!$result) { echo("Error performing query: " . mysql_error() . ""); exit(); } // Display the text while ( $row = mysql_fetch_array($result) ) { echo("" . $row["title"] . ""); } if ($row["title"] == "") echo ("Empty!") -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] verify text in field
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. > > // Request the text > $text = "Joe of Egypt"; > $sql = "SELECT title FROM book WHERE title LIKE '$text'"; > $result = mysql_query($sql); > if (!$result) { > echo("Error performing query: " . > mysql_error() . ""); > exit(); > } > > // Display the text > while ( $row = mysql_fetch_array($result) ) { > echo("" . $row["title"] . ""); > } > 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 pg_num_rows(). Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] verify text in field
Try $text = "Joe of Egypt"; $sql = "SELECT title FROM book WHERE title LIKE '$text'"; $result = mysql_query($sql); If(mysql_num_rows($result) >= '1') { while ( $row = mysql_fetch_array($sql) ) { echo("" . $row["title"] . ""); } } My Ideas my not make some people happy with my design but I use it and it works very well -Original Message- From: PJ [mailto:af.gour...@videotron.ca] Sent: Thursday, February 26, 2009 5:07 PM To: php-general@lists.php.net Subject: [PHP] verify text in field 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. Error performing query: " . mysql_error() . ""); exit(); } // Display the text while ( $row = mysql_fetch_array($result) ) { echo("" . $row["title"] . ""); } if ($row["title"] == "") echo ("Empty!") ?> -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] verify text in field
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. Error performing query: " . mysql_error() . ""); exit(); } // Display the text while ( $row = mysql_fetch_array($result) ) { echo("" . $row["title"] . ""); } if ($row["title"] == "") echo ("Empty!") ?> -- Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php