danp Posted December 14, 2009 Share Posted December 14, 2009 After looking around for about an hour on various sites, I can't seem to find a direct answer, only how to grab the entire array etc... I'm trying to find out how to compare a variable with a member of an array. Doing something similar to $idcheckx = mysql_query("SELECT id FROM inventory WHERE owner = '$user_name'"); $idchecky = mysql_fetch_assoc($idcheckx); would create an array with all the id's pertaining to the user... How would I check if a variable, let's say $this_id = "3"; exists in the $idchecky array? Thank you in advance, -D Quote Link to comment https://forums.phpfreaks.com/topic/185134-array-comparisons-with-mysql-array-and-a-php-variable/ Share on other sites More sharing options...
danp Posted December 14, 2009 Author Share Posted December 14, 2009 it seems I should use the "is_array" function, but the posts on php.net show how to do some tricky stuff with it... can anyone help me in plain English? Quote Link to comment https://forums.phpfreaks.com/topic/185134-array-comparisons-with-mysql-array-and-a-php-variable/#findComment-977264 Share on other sites More sharing options...
mattal999 Posted December 14, 2009 Share Posted December 14, 2009 $idcheckx = mysql_query("SELECT id FROM inventory WHERE owner = '$user_name'"); $idchecky = mysql_fetch_assoc($idcheckx); $thisid = 3; foreach($idchecky as $name => $id) { if(in_array($thisid, $id)) { echo "Found a match where id is ".$id; } } Quote Link to comment https://forums.phpfreaks.com/topic/185134-array-comparisons-with-mysql-array-and-a-php-variable/#findComment-977266 Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2009 Share Posted December 14, 2009 1) A call to mysql_fetch_assoc() fetches ONE row from the result set. 2) Since your query is only selecting id, $idchecky will be an array with one element $idchecky['id'] that will contain whatever your id column consists of for the specific owner = that matched the WHERE clause. What exactly does the data in your table look like (post at least two rows that should match a specific owner.) One of the great points of using a database is you only retrieve the information you are interested in. If you find yourself looping through the result of a query searing for a value, you are not using the database effectively. If you only want the row(s) that have a specific value, such as id = $this_id, then you would include that comparison in the WHERE clause in the query. Quote Link to comment https://forums.phpfreaks.com/topic/185134-array-comparisons-with-mysql-array-and-a-php-variable/#findComment-977289 Share on other sites More sharing options...
danp Posted December 14, 2009 Author Share Posted December 14, 2009 Thanks Matt & PFM! Quote Link to comment https://forums.phpfreaks.com/topic/185134-array-comparisons-with-mysql-array-and-a-php-variable/#findComment-977378 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.