jitu Posted May 27, 2007 Share Posted May 27, 2007 I have a table with say 2000 members name. Now when the viewers search for a name by first name and doesn't find that he got some suggestion for similar user name/ nick name. Problem is when I have say 50 people with same nick name what should be the query statement. I worte something as below: $result = mysql_query("SELECT * FROM theTable WHERE nickname='{$_GET['who']}' ORDER BY id ASC") It shows me something like this: Nick ID xyz 123456789 xyz 123456789 xyz 123456789 xyz 123456789 But I nedd something like Nick ID xyz 123456789 xyz 987456321 xyz 521478935 xyz 357412599 What should be the statement ? Please help me. Link to comment https://forums.phpfreaks.com/topic/53206-help-with-sql_query-where-statement/ Share on other sites More sharing options...
Barand Posted May 27, 2007 Share Posted May 27, 2007 try $result = mysql_query("SELECT DISTINCT * FROM theTable WHERE nickname='{$_GET['who']}' ORDER BY id ASC") Link to comment https://forums.phpfreaks.com/topic/53206-help-with-sql_query-where-statement/#findComment-262872 Share on other sites More sharing options...
jitu Posted May 28, 2007 Author Share Posted May 28, 2007 Didn't work ??? :-[ Link to comment https://forums.phpfreaks.com/topic/53206-help-with-sql_query-where-statement/#findComment-263337 Share on other sites More sharing options...
Illusion Posted May 28, 2007 Share Posted May 28, 2007 It shows me something like this: Nick ID xyz 123456789 xyz 123456789 xyz 123456789 xyz 123456789 are these resutls are seen from CUI or PHP. If PHP, post the code. Link to comment https://forums.phpfreaks.com/topic/53206-help-with-sql_query-where-statement/#findComment-263346 Share on other sites More sharing options...
jitu Posted May 28, 2007 Author Share Posted May 28, 2007 The code om simplest form: <?php //call datatbase $result = mysql_query("SELECT * FROM theTableName WHERE nickname='{$_GET['who']}' ORDER BY id ASC") or die(mysql_error()); $row = mysql_fetch_array($result); //start table echo("<table boder='0' cellpadding='0' cellspacing='3' width='500'><tr>"); //asign variable $num = mysql_num_rows($result); $i=0; //loop start while ($i < $num) { echo("<td width='50%' align='center'>".$row['nick']."</td><td width='50%' align='center'>".$row['id']."</td>"); $i++; } //end table echo("</tr></table>"); ?> Link to comment https://forums.phpfreaks.com/topic/53206-help-with-sql_query-where-statement/#findComment-263433 Share on other sites More sharing options...
Barand Posted May 28, 2007 Share Posted May 28, 2007 fetch_array statement needs to be inside the loop, not before it, otherwise you just read the first record and repeat its contents. <?php while ($i < $num) { $row = mysql_fetch_array($result); echo("<td width='50%' align='center'>".$row['nick']."</td><td width='50%' align='center'>".$row['id']."</td>"); $i++; } Link to comment https://forums.phpfreaks.com/topic/53206-help-with-sql_query-where-statement/#findComment-263445 Share on other sites More sharing options...
jitu Posted May 28, 2007 Author Share Posted May 28, 2007 Sorry for my fault, I mistype while re-writing, In my original script it is inside the loop, corrected code <?php //call datatbase $result = mysql_query("SELECT * FROM theTableName WHERE nickname='{$_GET['who']}' ORDER BY id ASC") or die(mysql_error()); //start table echo("<table boder='0' cellpadding='0' cellspacing='3' width='500'><tr>"); //asign variable $num = mysql_num_rows($result); $i=0; //loop start while ($i < $num) { $row = mysql_fetch_array($result); echo("<td width='50%' align='center'>".$row['nick']."</td><td width='50%' align='center'>".$row['id']."</td>"); $i++; } //end table echo("</tr></table>"); ?> But still it's not working!! Link to comment https://forums.phpfreaks.com/topic/53206-help-with-sql_query-where-statement/#findComment-263459 Share on other sites More sharing options...
fenway Posted May 31, 2007 Share Posted May 31, 2007 Why wouldn't you just use a while ( $row = mysql_fetch_assoc( $result ) )? Link to comment https://forums.phpfreaks.com/topic/53206-help-with-sql_query-where-statement/#findComment-265509 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.