Jump to content

Recommended Posts

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.

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?

 

 

 

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?

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']."';

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.