drd2551 2014-03-06 02:40
浏览 381

fetchAll()返回值为0的数组(PDO)

I'm trying to bind values into a SQL statement. It appears it is either not binding, or is working and is not returning the needed data for reasons unbeknownst to me. Here is my code:

$search_string = 'turkey';

// Gathers the bird ID through searched name
$birdID = $conn->prepare("SELECT `id` FROM `birds` WHERE `bird_name` LIKE :birdName ;");    
$birdID->execute(array(':birdName' => '%'.$search_string.'%'));  
$returnBirdID = $birdID->fetchAll(PDO::FETCH_BOTH);

foreach($returnBirdID as $birdID){
    $birdsID[] = $birdID;
}

Important to note here is var_dump($birdsID) returns an array as expected.

I have another code block similar to the above that retrieves data based on state abbreviation, e.g. "NY". Leaving it out for brevity (it's almost identical to the above).

// Build query for binding.

$sql = "
              SELECT t.state_id,t.bird_id
              FROM table1
              WHERE bird.id = :birdID 
              AND states.id = :statesID ;
          ";
$query = $conn->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$query->execute(array(':birdID'=>$birdsID,':statesID'=>$statesID));// Think this line may be the problem...
$results = $query->fetchAll(PDO::FETCH_BOTH);

foreach($results as $getID){
    $getIDs[] = $getID;
}

var_dump($getIDs) returns NULL here. I think it may be tied into :birdID'=>$birdsID not accepting $birdsID as a value because all by itself it is an array. But coding it as $birdsID[0] does not work either. Not sure what's up!

Whew... been working on this several hours. If it doesn't make sense, sorry! Brain fried.

  • 写回答

1条回答 默认 最新

  • douluogu8713 2014-03-06 10:31
    关注

    In the PDO tag (info) you will find the correct procedure for using wildcards in parameters. PDO Tag

    $search_string = '%turkey%';
    
    // Gathers the bird ID through searched name
    $birdID = $conn->prepare("SELECT `id` FROM `birds` WHERE `bird_name` LIKE :birdName ;");    
    $birdID->execute(array(':birdName' => $search_string));  
    $returnBirdID = $birdID->fetchAll(PDO::FETCH_BOTH);
    
    foreach($returnBirdID as $birdID){
        $birdsID[] = $birdID;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法