Compare string with MySQL result array

A

Anonymous

Guest
What is the simplest way to compare a string variable with a MySQL result array?
 
Not sure about what you want to do, but you can make something like:
Code:
<?php
// Your string:
$name = 'John';

$result = mysql_query("SELECT name FROM table WHERE id=8");
$array = mysql_fetch_array($result);

// Let´s say the returned 'name' is John!
// The $array will be:
// array ([0] => "John", ['name'] => "John")

// So you can do:

if($name == $array['name']){
// Do something
}

// Or...

if($name == $array[0]){
// Do something
}

?>
 
The problem with the above is that I have more than one result from the query(which is very similar) - I have tried using in_array but am not sure how this should be applied.

Thanks!
 
By far the best way to do this is in your MySQL query, before PHP gets the results. If you can give a broader description of what you're trying to accomplish, I can give you some more specific advice. MySQL has scores of string-manipulation functions and string-matching functionality.
 
Essentially I have a table with cities/zip codes - some zip codes will return two or three cities - I have a form which includes zip code and city fields and wish to compare the posted city against my db cities according to the posted zip (the zip has been validated before the cities query) - if the posted city is not valid it will return a drop menu of valid cities (I don't need help with this).

I can now see how I could do this simply with the query - thankyou :)

I would still like to know how to compare the posted variable to the results in the array using PHP :oops:

Thanks!
 
you can try the function "array_search" to find if the resutls are there in the resultset
 
Back
Top