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

4

2 回答 2

0

首先,您必须在服务器代码中获取 Android 客户端的 IP 地址。在 PHP(源代码)中:

$client_ip = $_SERVER['REMOTE_ADDR']

或者,如果您在 ELB 后面有多个 Apache Web 服务器():

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

现在您的 PHP 代码可以根据$client_ip. 如果地址是 Sam 的,请运行select * from sam,如果是 Carl 的,请运行select * from carl。如果它是未知的 IP 地址,那么您可以使用错误消息进行响应。

无论如何,我不认为基于 IP 的识别是一个好主意。如果 Carl 或 Sam 重新启动他的 Android 手机,其 IP 地址将更改,连接将不再有效。

您的数据库设计也存在问题。例如,如果您当前的 Sam 和 Carl 表包含聊天消息,那么以下模式会更好:

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

对于这些表,列出当前用户消息的查询如下所示:

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

'xxx.xxx.xxx.xxx'将是 的值$client_ip

于 2013-08-27T11:53:45.773 回答
0

为此,您必须再需要一张表来存储 IP 地址及其表名并创建这样的查询

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

并在第二个查询中使用该表名

select * from tablename 
于 2013-08-27T11:33:05.607 回答