使用 MySQL 查询获取 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21777200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:02:55  来源:igfitidea点击:

Getting ip address using MySQL query

mysqlsqlip-address

提问by Jesika

In MySQL query:

在 MySQL 查询中:

SELECT host
FROM information_schema.processlist
WHERE ID = connection_id( )
LIMIT 0 , 30

The result of ^ this query is: localhost.

^ 这个查询的结果是:localhost

SELECT @@hostname;

The result of ^ this query is: localhost.

^ 这个查询的结果是:localhost

But I need to get ipaddress like 192.168.1.2.

但我需要获得 ipaddress 之类的192.168.1.2

Question:How to get this result using mysql query?

问题:如何使用mysql查询得到这个结果?

采纳答案by Vignesh Kumar A

To get the IP address only without the port number.

仅获取 IP 地址,而无需端口号。

 Select SUBSTRING_INDEX(host,':',1) as 'ip' 
 From information_schema.processlist 
 WHERE ID=connection_id();

回答by Nisham Mahsin

The query
select host from information_schema.processlist WHERE ID=connection_id();

查询
select host from information_schema.processlist WHERE ID=connection_id();

Will give you the host name .You will get IP address( like 192.168.1.2.) if name resolution is not enabled, which it is usually not.

将为您提供主机名。192.168.1.2.如果未启用名称解析,您将获得 IP 地址(如),通常不会。

回答by user3162968

Please tell me whether you need to get IP from visitor of the site, or something else. If you just need IP of the visitor, you could use php remote_addr function.

请告诉我您是否需要从网站访问者那里获取 IP 或其他内容。如果您只需要访问者的 IP,您可以使用 php remote_addr 函数。

$_SERVER['REMOTE_ADDR'];

Is it something like a table where IP adresses are stored? I think that your query is looking for host, not for IP. But I am not sure if I understood you properly.

它是否类似于存储 IP 地址的表?我认为您的查询正在寻找主机,而不是 IP。但我不确定我是否正确理解你。