1

Assume there is client and a server

Client:: Android mobile

Server:: AWS server

Server has mysql database installed in it

Database name:: Person

Tables in person Database:: "sam" and "carl"

Now mobile sends a request...

  • How to write an SQL query so that if mobile sends request i/p as "sam" display all the values of sam table
  • But if the mobile sends an i/p as carl then dipslay all the values of carl table

[Note] I have used statement SELECT * FROM TABLE_NAME

What i am trying to achieve is a condition response when a client request comes in


I/p means :: say i get carl as ip .... i want to perform one query else sam as input ... i want to perform another query

what query statement should i need to write ?


hope i am stating my problem correctly

Thanks

Strawberry
  • 33,750
  • 13
  • 40
  • 57

2 Answers2

0

For this u have to require one more table where you store IP address and their table name and create query like this

Select tableName from IPTable where ip= 'xxx.xxx.xxx.xxx'

and using that table name in your second query

select * from tablename 
Varun
  • 373
  • 1
  • 2
  • 11
  • Thanks for your i/p ..... but can you update your answer on merging those two queries into single query if its possible technically ! –  Aug 27 '13 at 11:51
  • as far i know it's not possible, Please mark answer for help to some one else. – Varun Aug 27 '13 at 12:20
0

First, you have to get the IP address of the Android client in your server code. In PHP (source):

$client_ip = $_SERVER['REMOTE_ADDR']

Or, if you have multiple Apache web servers behind ELB (source):

$http_headers = apache_request_headers(); 
$client_ip = $http_headers["X-Forwarded-For"];

Now your PHP code can choose which query to run according to the value of $client_ip. If the address is Sam's, run select * from sam, if it is Carl's, run select * from carl. If it's an unknown IP address, then you can respond with an error message.

Anyway, I don't think IP-based identification is a good idea. If Carl or Sam reboots his Android phone, its IP address will change, and the connection will no longer work.

There are problems with your database design, too. For example, if your current Sam and Carl tables contain chat messages, then the following schema would be better:

- User table: ID, Name, IPAddress 
- Message table: ID, UserID, SendingTime, Text

For these tables, the query which lists the messages of the current user would look like this:

SELECT User.Name, Message.SendingTime, Message.Text
FROM User, Message
WHERE User.ID = Message.UserID AND User.IPAddress = 'xxx.xxx.xxx.xxx'

Here 'xxx.xxx.xxx.xxx' would be the value of $client_ip.

Community
  • 1
  • 1
kol
  • 27,881
  • 12
  • 83
  • 120