PHP MySQL - Do a locate/find row in mysql results set?

Marius0188
Marius0188 used Ask the Experts™
on
Hi Experts,

How can I do a locate on a field in a mysql result using PHP?
Let's say I have a query pulling a list of clients but now I want to
check whether clientid 212 do exist in the result set.

What is the short way to do this instead of looping the result.

For example:
if (mysql_locate('fieldname', 'fieldvalue')) {
 echo "record do exist!";
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
The best way would be to put it in the query with a WHERE clause:

SELECT * FROM Table WHERE clientid=212
Basically, there is no short way to do this.  Even if there was a function that did this, it would loop through the result.  You can follow yondrcm's example but that is different since you already have all the data pulled from database.

You will have to pull all the data from mysql result set into a multidimensional array and then search on one of the columns.  

Commented:
Well, if you have the entire result set as $result, you can always make it a keyed array by

$array = mysql_fetch_array($result);

Then this would work:


if (in_array(121, $array))
echo "true";
else
echo "false";

But it would find ANY value of 121, not guaranteed to be in a particular field.  If your data is such that you can guarantee the value will not show up in any other field, you could use it.

Author

Commented:
The problem is I need to check on a certain field.
I can not be sure that the value will not also be in another field.

So what would be your suggestions on the best way to do this?
Loop through the result???

Thanks for the help so far.
Commented:
Yes, loop though would be the obvious and easiest answer, and it won't make much difference in time.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial