crashmaster Posted November 18, 2007 Share Posted November 18, 2007 Hi there, I have one problem/ ex. of mysql database 'test' ----------------- id: 1 , name:Admin id: 2 , name: user1 id: 3 , name: Admin id: 4 , name: user2 id: 5 , name: user1 ----------------- ex. of the mysql_qery $query = mysql_query("SELECT name FROM test"); What I need: I need to create array $names, where each 'name' will be only once. I've tried this, but it doesnt work while ( $z = array_unique(mysql_fetch_array($query)) ) { echo $z[name]; } I need to have output like this: $names = array ('Admin', 'user1', 'user2'); Can somebody help me... Quote Link to comment https://forums.phpfreaks.com/topic/77833-solved-uniqe-array/ Share on other sites More sharing options...
rajivgonsalves Posted November 18, 2007 Share Posted November 18, 2007 you should fetch the array fetch and then try a array_unique you may have multiple records Quote Link to comment https://forums.phpfreaks.com/topic/77833-solved-uniqe-array/#findComment-393961 Share on other sites More sharing options...
crashmaster Posted November 18, 2007 Author Share Posted November 18, 2007 can you write me example of the code ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/77833-solved-uniqe-array/#findComment-393963 Share on other sites More sharing options...
wsantos Posted November 18, 2007 Share Posted November 18, 2007 Why not do it from the query itself? It seems you are just getting the name column $query = mysql_query("SELECT DISTINCT name FROM test"); That should get the unique data Quote Link to comment https://forums.phpfreaks.com/topic/77833-solved-uniqe-array/#findComment-394022 Share on other sites More sharing options...
toplay Posted November 18, 2007 Share Posted November 18, 2007 Just use the DISTINCT keyword in your query. $query = mysql_query("SELECT DISTINCT name FROM test"); http://dev.mysql.com/doc/refman/5.0/en/select.html EDIT: wsantos beat me to it. Quote Link to comment https://forums.phpfreaks.com/topic/77833-solved-uniqe-array/#findComment-394023 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.