0

Can sumone please give me a helping hand here am trying to do a simple sql search were it will look all items in SQL database and pull up the names that match but am having a abit of trouble with it it brings all my accounts up regardless of there name can anyone help?

procedure TMainServer.SearchUserAccounts(const MyUserID, UserID, Token: String; rSocket: TWSocketClient);
var
  search, msg: string;
begin
  case StrToInt(Token) of
   0: search := Trim(UserID);
   1: search := Trim(UserID);
   2: search := Trim(UserID);
  end;

  MyQuery.SQL.Text := 'SELECT * FROM users';
  try
   MyQuery.Open;
   MyQuery.First;

  while not (MyQuery.Eof) do begin

  if (Token = '0') and ContainsText(MyQuery.Fields[1].AsString, search) then begin // check username
       msg := '';
       msg := msg + 'š24œ' + MyQuery.Fields[1].AsString + 'œ' + MyQuery.Fields[11].AsString + 'œ' + MyQuery.Fields[6].AsString + 'œ' + MyQuery.Fields[14].AsString + 'œ' + MyQuery.Fields[3].AsString + 'œ' + MyQuery.Fields[3].AsString + 'œ';
    if msg > '' then
      SocketSendCodedString(rSocket, msg);
  end;

  if (Token = '1') and ContainsText(MyQuery.Fields[14].AsString, search) then begin // check ip
       msg := '';
       msg := msg + 'š24œ' + MyQuery.Fields[1].AsString + 'œ' + MyQuery.Fields[11].AsString + 'œ' + MyQuery.Fields[6].AsString + 'œ' + MyQuery.Fields[14].AsString + 'œ' + MyQuery.Fields[3].AsString + 'œ' + MyQuery.Fields[3].AsString + 'œ';
    if msg > '' then
      SocketSendCodedString(rSocket, msg);
  end;

  if (Token = '2') and (MyQuery.Fields[11].AsString = search) then begin // check rank
       msg := '';
       msg := msg + 'š24œ' + MyQuery.Fields[1].AsString + 'œ' + MyQuery.Fields[11].AsString + 'œ' + MyQuery.Fields[6].AsString + 'œ' + MyQuery.Fields[14].AsString + 'œ' + MyQuery.Fields[3].AsString + 'œ' + MyQuery.Fields[3].AsString + 'œ';
    if msg > '' then
       SocketSendCodedString(rSocket, msg);
    end;

    MyQuery.Next;

   end finally
    MyQuery.Close;
  end;
end;

thanks

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by pritaeas
1

This should do exactly the same:

procedure TMainServer.SearchUserAccounts(const MyUserID, UserID, Token: String; rSocket: TWSocketClient);
var
  search, msg: string;
begin
  search := Trim(UserID);
  
  MyQuery.SQL.Text := 'SELECT * FROM users ';
  case StrToInt(Token) of
    0: MyQuery.SQL.Text := 'WHERE username LIKE ''%' + search + '%''';
    1: MyQuery.SQL.Text := 'WHERE ip LIKE ''%' + search + '%''';
    2: MyQuery.SQL.Text := 'WHERE rank = ''' + search + '''';
  end;
  
  try
    MyQuery.Open;
    MyQuery.First;
 
    while not MyQuery.Eof do begin
      msg := 'š24œ' + 
             MyQuery.Fields[1].AsString + 'œ' + 
             MyQuery.Fields[11].AsString + 'œ' + 
             MyQuery.Fields[6].AsString + 'œ' + 
             MyQuery.Fields[14].AsString + 'œ' + 
             MyQuery.Fields[3].AsString + 'œ' + 
             MyQuery.Fields[3].AsString + 'œ';
             
      SocketSendCodedString(rSocket, msg);
      MyQuery.Next;
    end; 
  finally
    MyQuery.Close;
  end;
end;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.