doujing1156 2015-09-28 18:15
浏览 47

防止“注意:数组转换为字符串”[复制]

This question already has an answer here:

I need help with prevent from "Notice: Array to string conversion in "

$stmt = $DBH->prepare("SELECT * FROM users WHERE id = :emailid");
$stmt->bindParam(':emailid', $emailid, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();    
foreach($result as $row)
    {
    echo $row['id'].'<br />';
    echo $row['emailid'].'<br />';
    echo $row['password'];
    }

Everythink its OK, index.php?id=5

but when i put index.php?id[]=5

Return me error

Notice: Array to string conversion in 

What is the best way to check for arrays in this case; error_reporting(0) is not a solution I want to find optimized method.

</div>
  • 写回答

1条回答 默认 最新

  • dt1888 2015-09-28 18:27
    关注

    Not sure in which direction you want this question to go. But let's assume you want - for some reason - make it work either way: ?id=... and ?id[]=...&id[]=...

    if ( !is_array($_GET['id']) ) {
        // just make it an array with one element
        $ids = array( $_GET['id'] );
        // so having a simple type string id is just a special case of the 
        // array case
    }
    else {
        $ids = $_GET['id'];
    }
    
    $stmt = $DBH->prepare("SELECT * FROM users WHERE id = :emailid");
    $stmt->bindParam(':emailid', $emailid, PDO::PARAM_STR);
    foreach( $ids as $emailid ) {
        $stmt->execute();
        $result = $stmt->fetchAll();    
        foreach($result as $row) {
            ...
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'