Skylight_lady Posted January 5, 2011 Share Posted January 5, 2011 Hi, I'm am trying to use a search engine that search's for a username in the database and displays the information back. I have searched for a script but none of them has helped me. I have tried to use this without any luck: mysql_connect("localhost", "", "") or die(mysql_error()); mysql_select_db("dbsystem") or die(mysql_error()); $todo=$_POST['todo']; if(isset($todo) and $todo=="search"){ $search_text=$_POST['search_text']; $type=$_POST['type']; $search_text=ltrim($search_text); $search_text=rtrim($search_text); if($type<>"any"){ $query="select * from users where name = '$search_text'"; }else{ $kt=split(" ",$search_text);//Breaking the string to array of words // Now let us generate the sql while(list($key,$val)=each($kt)){ if($val<>" " and strlen($val) > 0){$q .= " name like '%$val%' or ";} }// end of while $q=substr($q,0,(strlen($q)-3)); // this will remove the last or from the string. $query="select * from users where $q "; } // end of if else based on type value echo $query; echo "<br><br>"; $nt=mysql_query($query); echo mysql_error(); while($row=mysql_fetch_array($nt)){ echo "$row[name]<br>"; } // End if form submitted }else{ echo "<form method='post' action=''><input type='hidden' name='todo' value='search' /> <input type='text' name='search_text' /><input type='submit' value='Search' /><br> <input type='radio' name='type' value='any' checked />Match any where <input type='radio' name='type' value='exact' />Exact Match </form> "; } I will be grateful if you can help with this. Quote Link to comment https://forums.phpfreaks.com/topic/223431-mysql-database-search-engine-on-username/ Share on other sites More sharing options...
requinix Posted January 5, 2011 Share Posted January 5, 2011 Without any luck... how? Did you just copy/paste the code and expect it to automagically work? Did you make changes to whatever script your found and are now getting error messages? The wrong results? Quote Link to comment https://forums.phpfreaks.com/topic/223431-mysql-database-search-engine-on-username/#findComment-1155033 Share on other sites More sharing options...
Zurev Posted January 5, 2011 Share Posted January 5, 2011 Without any luck... how? Did you just copy/paste the code and expect it to automagically work? Did you make changes to whatever script your found and are now getting error messages? The wrong results? It comes from http://www.plus2net.com/sql_tutorial/search-keyword.php Well first I'm curious of something, are you searching for a username that is similar to the users username, or identical? I.E. someone types in Skylight and returns similar usernames including skylight_lady, or they search skylight and get the user skylight? Quote Link to comment https://forums.phpfreaks.com/topic/223431-mysql-database-search-engine-on-username/#findComment-1155037 Share on other sites More sharing options...
Skylight_lady Posted January 5, 2011 Author Share Posted January 5, 2011 Hi guys, sorry. wrong search code i used ha. I might as well use this one as it seems like the only one that works. Quote Link to comment https://forums.phpfreaks.com/topic/223431-mysql-database-search-engine-on-username/#findComment-1155307 Share on other sites More sharing options...
Maq Posted January 5, 2011 Share Posted January 5, 2011 Hi guys, sorry. wrong search code i used ha. I might as well use this one as it seems like the only one that works. It's not going to work CnP, you will have to fix it up to work. Have you got everything working? Quote Link to comment https://forums.phpfreaks.com/topic/223431-mysql-database-search-engine-on-username/#findComment-1155422 Share on other sites More sharing options...
Skylight_lady Posted January 6, 2011 Author Share Posted January 6, 2011 It doesn't fully work. I have messed around with it. The problem i'm getting is that It is not getting the results from 2 fields in the table even tho i have added another field to the sql statement. It's displaying all the results from the table while i want the results to be smaller and by userID as well. Here is my example: mysql_connect("localhost", "", "") or die(mysql_error()); mysql_select_db("dbsystem") or die(mysql_error()); $query = "SELECT ID FROM clients WHERE username = '$username'"; $result = mysql_query($query); if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)){ $userID = $row['ID']; $todo=$_POST['todo']; if(isset($todo) and $todo=="search"){ $search_text=$_POST['search_text']; $type=$_POST['type']; $search_text=ltrim($search_text); $search_text=rtrim($search_text); if($type<>"any"){ echo $userID; $query="select * from users where name = '$search_text'" AND userID = '".$row['ID']."'; $result = mysql_query($query); } else if ($search_text == ""){ echo "You have an error in your search form"; }else{ $kt=split(" ",$search_text);//Breaking the string to array of words // Now let us generate the sql while(list($key,$val)=each($kt)){ if($val<>" " and strlen($val) > 0){$q .= " name like '%$val%' or ";} }// end of while $q=substr($q,0,(strlen($q)-3)); // this will remove the last or from the string. $query="SELECT * FROM users WHERE $q AND userID = '$userID' "; } // end of if else based on type value echo "<br><br>"; $nt=mysql_query($query); while($row=mysql_fetch_array($nt)){ $userID = $row['userID']; echo $userID; echo "$row[name]<br>"; } // End if form submitted }else{ echo "<form method='post' action=''><input type='hidden' name='todo' value='search' /> <input type='text' name='search_text' /><input type='submit' value='Search' /><br> <input type='radio' name='type' value='any' checked />Match any where <input type='radio' name='type' value='exact' />Exact Match </form> "; }}} How do i sort this? Quote Link to comment https://forums.phpfreaks.com/topic/223431-mysql-database-search-engine-on-username/#findComment-1155465 Share on other sites More sharing options...
Zurev Posted January 6, 2011 Share Posted January 6, 2011 There's no way it's not erroring out with that second select query...you're blatantly ending the double quote before the string is over and tacking an AND clause onto it. $query="select * from users where name = '$search_text' AND userID = '".$row['ID']."'; Quote Link to comment https://forums.phpfreaks.com/topic/223431-mysql-database-search-engine-on-username/#findComment-1155498 Share on other sites More sharing options...
Skylight_lady Posted January 6, 2011 Author Share Posted January 6, 2011 Hi, thanks for your reply. That was my mistake when i edited the code for you to view it. That double quote is not in the original code. Thanks for correcting this. However, even when that sql statement works, it is still not getting all the users with a userID. Even when i set that to 10. Quote Link to comment https://forums.phpfreaks.com/topic/223431-mysql-database-search-engine-on-username/#findComment-1155655 Share on other sites More sharing options...
Skylight_lady Posted January 7, 2011 Author Share Posted January 7, 2011 Thanks guys. I finally got it working. I wrote the code from scratch. Only about ten lines associated with it. Tho, I'll be coming back to this code as shown here at some stage to try and sort this one. It's not needed but I don't leave any codes unsolved. Quote Link to comment https://forums.phpfreaks.com/topic/223431-mysql-database-search-engine-on-username/#findComment-1156002 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.