douwen1213 2017-10-13 02:09
浏览 10
已采纳

PHP是否可以从表中检查变量是数组的数据?

Is it possible to check data from table where variable is an array?

Example:

$allowedIP = array();
$qChkIP = oci_parse($c1, "SELECT ALLOWED_IP_ADDRESS FROM WA_BT_TBL_PROFILE WHERE ACTIVE = 'Y'");
oci_execute($qChkIP);
while($dChkIP = oci_fetch_array($qChkIP))
{
    $allowedIP = array_merge($allowedIP, array_map('trim', explode(',', $dChkIP['ALLOWED_IP_ADDRESS'])));
}

print_r $allowedIP

Array ( [0] => 192.168.183.205 [1] => 192.168.184.20 [2] => 192.168.184.15 [3] => 192.168.183.28 )

My current IP Address is: 192.168.183.28

and now I want to get detail information from table where ALLOWED_IP_ADDRESS = $allowedIP

SELECT P.ALLOWED_IP_ADDRESS, P.PROFILEID, P.PROFILE_NAME, P.LOCATIONID_FK, L.LOCATIONID, L.LOCATIONNAME FROM WA_BT_TBL_PROFILE P, WA_GA_TBL_LOCATIONS L WHERE P.LOCATIONID_FK = L.LOCATIONID AND P.ACTIVE = 'Y' AND P.ALLOWED_IP_ADDRESS IN (".implode(',',$allowedIP).")

But there is no result.

UPDATED

DATA

PROFILEID   PROFILE_NAME        ACTIVE  DATEADDED               ADDEDBY LOCATIONID_FK   ALLOWED_IP_ADDRESS
PF0001      Normal Working Day  Y       9/30/2017 5:53:39 PM    US0001  LC0001          192.168.183.205
PF0004      Ramadhan            N       10/12/2017 10:38:02 AM  US0001  LC0003  
PF0002      Ramadhan            N       9/30/2017 5:55:50 PM    US0001  LC0001          192.168.183.205
PF0003      Normal Working Day  Y       10/3/2017 5:23:05 PM    US0001  LC0003          192.168.184.20, 192.168.183.28

I'm using this method from Randall

"... P.ALLOWED_IP_ADDRESS IN ('".implode("','",$allowedIP)."') ..."

and I can see the data but result is not correct. I got "PF0001", should be "PF0003"

  • 写回答

1条回答 默认 最新

  • duanrou5680 2017-10-13 02:40
    关注

    You need to single quote each IP in the "IN" value, you have:

    "... P.ALLOWED_IP_ADDRESS IN (".implode(',',$allowedIP).") ..."
    

    Which would make something like this (which is invalid sql):

    P.ALLOWED_IP_ADDRESS IN (11.11.11.11,22.22.22.22,33.33.33.33)
    

    A basic way is like this:

    "... P.ALLOWED_IP_ADDRESS IN ('".implode("','",$allowedIP)."') ..."
    

    Which ends up as (more valid sql):

    P.ALLOWED_IP_ADDRESS IN ('11.11.11.11','22.22.22.22','33.33.33.33')
    

    You can get more involved if you wish to sanitize the data for safety, but I think thats beyond the need of the answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀