dk4210 Posted January 20, 2012 Share Posted January 20, 2012 Hello Guys, I have a question I have the following query $result3 = mysql_query("SELECT * FROM table1 WHERE ad_id='$id2'") or die(mysql_error()); $row3 = mysql_fetch_array( $result3 ); // Grab all the var $features = $row3['features']; I am turning it into an array (comma separated) $feature2 = explode(",", $features); print_r($feature2); ) The result is like so Array ( [0] => 5 [1] => 9 [2] => 13 I want to query the features for just the ids (F_name) are for the features . This query will show all.. I would like to just display the f_name values from the array query. // build and execute the query $sql = "SELECT * FROM features"; $result = mysql_query($sql); // iterate through the results while ($row = mysql_fetch_array($result)) { echo $row['f_name']; echo "<br />"; } Please Advise.. Thanks, Dan Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/ Share on other sites More sharing options...
bspace Posted January 20, 2012 Share Posted January 20, 2012 F_name or f_name ? watch your case what's the connection between your first bunch of code and the last please explain what you want as it's not clear from your post Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1309547 Share on other sites More sharing options...
dk4210 Posted January 20, 2012 Author Share Posted January 20, 2012 Basically, I am querying one table and getting the id(s) of features (see attachment #1) Then I want to query the "Features" table for the names (See attachment #2) Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1309549 Share on other sites More sharing options...
bspace Posted January 20, 2012 Share Posted January 20, 2012 use IN something like: $feature2 = explode(",",$feature2) $sql = "SELECT * FROM features WHERE f_id IN $feature2"; Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1309562 Share on other sites More sharing options...
dk4210 Posted January 20, 2012 Author Share Posted January 20, 2012 Hi Bspace, That didn't work.. Nothing was printed from the while loop Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1309592 Share on other sites More sharing options...
PFMaBiSmAd Posted January 20, 2012 Share Posted January 20, 2012 While the suggested method is OK, the specific syntax is not - http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in The IN() comparison takes a comma separate list of numbers (or strings), not an array. Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1309593 Share on other sites More sharing options...
dk4210 Posted January 20, 2012 Author Share Posted January 20, 2012 So what can be used instead of IN for arrays? Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1309595 Share on other sites More sharing options...
bspace Posted January 20, 2012 Share Posted January 20, 2012 that was the point of turning the array into a comma separate list of numbers as in $feature2 = explode(",",$feature2); it got lost out of the "code" box, but it's there in my post so you get $feature2 = explode(",",$feature2); $sql = "SELECT * FROM features WHERE f_id IN $feature2"; which is using a comma separate list of numbers, as is needed, not an array sorry i messed the code box up, hopefully it's clear now Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1309598 Share on other sites More sharing options...
dk4210 Posted January 20, 2012 Author Share Posted January 20, 2012 Shouldn't it be $feature2 = explode(",", $features); Instead of $feature2 = explode(",", $features2); Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1309599 Share on other sites More sharing options...
PFMaBiSmAd Posted January 20, 2012 Share Posted January 20, 2012 explode produces an array from a string. That's NOT what you want, because there is no mysql query statement in existence that understands a php array variable. Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1309604 Share on other sites More sharing options...
bspace Posted January 20, 2012 Share Posted January 20, 2012 actually looking at it again there's no need for the array bit, and i completely b'd that up anyway cause explode isn't what's needed double sorry $features is a comma seperated list anyway, like (1,5,9) $sql = "SELECT * FROM features WHERE f_id IN $features"; should work Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1309607 Share on other sites More sharing options...
SergeiSS Posted January 20, 2012 Share Posted January 20, 2012 I'd say that your DB is not 'normalized'. You keep a list of features in one field. But you'd better keep it in different rows. Maybe you need one more table... Read about it, for example, here http://en.wikipedia.org/wiki/Database_normalization Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1309608 Share on other sites More sharing options...
dk4210 Posted January 23, 2012 Author Share Posted January 23, 2012 FYI - This is what I used to fix my issue $feature2 = explode(",", $features); $feature3 = implode(',', $feature2); // First Query to grab Feature1 $result = mysql_query("SELECT * FROM features WHERE f_id IN ( $feature3 ) ") or die("Sql error : " . mysql_error()); while($row = mysql_fetch_assoc ($result)){ $f_name=$row["f_name"]; echo "<br>".$f_name; } Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1310291 Share on other sites More sharing options...
PFMaBiSmAd Posted January 23, 2012 Share Posted January 23, 2012 Why would you explode data, only to implode it back to what it started as? Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1310293 Share on other sites More sharing options...
dk4210 Posted January 23, 2012 Author Share Posted January 23, 2012 Not sure why that is.. I found the fix on another site and it works fine...Just like I wanted it to.. Quote Link to comment https://forums.phpfreaks.com/topic/255423-mysql-query-from-array-values/#findComment-1310302 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.