PHP函数内部的PDO查询

时间:2012-11-23 23:11:42

标签: php function pdo

我正在创建这个函数,其中应该读取数组的一部分,并且每个值都包含一个我想要执行PDO查询的数字。这是我的以下代码:

function get_topics($array) {

$top = 20; $base = 0;
foreach ($array as $key => $value) {


$getData = $dbc->prepare('SELECT * FROM topics WHERE id = :id LIMIT 1'); 
$getData->execute(array(':id' => $value));

while($row = $getData->fetch()) {



$potential_topic_img = 'members/topic_' . $value . '.jpg'; 
if (file_exists($potential_topic_img)) { $topic_img = $potential_topic_img; } else {      
$topic_img = 'members/0.jpg'; } 




$name = $row['name'];
echo '<div class="topic_div"><img src="' . $topic_img . '" width="80"><br /><span 
style="font-size:10pt;">' . $name . '</span></div>';


} if (++$base == $top) break;

}

}

echo get_topics($some_array);

但我得到的是一个错误,告诉我:“解析错误:语法错误,/ home /......中的意外T_VARIABLE”,并且它说问题就在于此行:

$getData->execute(array(':id' => $value));

我可以做错什么?

修改

我删除了一些代码,剩下的代码运行正常:

function get_topics($array) {


foreach ($array as $key => $value) {

echo $value;

  }
}

echo get_topics($user_likes_array);

所以这不是$ value是空的,问题似乎是我在开头提到的那一行,因为当我移动该行下面的所有内容时,错误消息不会改变,但是当我移动时它确实会改变具体路线。

2 个答案:

答案 0 :(得分:1)

foreach ($array as $key => $value)在每次迭代时将当前元素的键分配给$value变量。

尝试

$getData->execute(':id',$value);

答案 1 :(得分:0)

您发布的代码是正确的,您确定您的代码是正确的。

我刚刚处理了你的代码并整理了布局并通过我的PHP测试批处理运行它并且它是正确的

测试批次的输出

G:\Others\Programs\phpApplications>SET PHP_PATH=../php/php.exe

G:\Others\Programs\phpApplications>SET FILE_PATH=../phpApplications/test2.php

G:\Others\Programs\phpApplications>SET LOOP=FALSE

G:\Others\Programs\phpApplications>"../php/php.exe" "../phpApplications/test2.ph
p"

G:\Others\Programs\phpApplications>pause
Press any key to continue . . .

使用的代码

<?php

function get_topics($array) {
    $top = 20; 
    $base = 0;
    foreach ($array as $key => $value) {
        $getData = $dbc->prepare('SELECT * FROM topics WHERE id = :id LIMIT 1'); 
        $getData->execute(array(':id' => $value));
        while($row = $getData->fetch()) {
            $potential_topic_img = 'members/topic_' . $value . '.jpg'; 
            if (file_exists($potential_topic_img)) { $topic_img = $potential_topic_img; } else {     
            $topic_img = 'members/0.jpg'; } 

            $name = $row['name'];
            echo '<div class="topic_div"><img src="' . $topic_img . '" width="80"><br /><span     
            style="font-size:10pt;">' . $name . '</span></div>';
        }
        if (++$base == $top) break;
    }
} 
$some_array = array();
echo get_topics($some_array);

如果在将暂停命令发送到CMD之前发生错误,则会显示错误

证明我的测试人员工作 为此我刚评论// $some_array = array()这里是结果

G:\Others\Programs\phpApplications>SET PHP_PATH=../php/php.exe

G:\Others\Programs\phpApplications>SET FILE_PATH=../phpApplications/test2.php

G:\Others\Programs\phpApplications>SET LOOP=FALSE

G:\Others\Programs\phpApplications>"../php/php.exe" "../phpApplications/test2.ph
p"

Warning: Invalid argument supplied for foreach() in G:\Others\Programs\phpApplic
ations\test2.php on line 6

G:\Others\Programs\phpApplications>pause
Press any key to continue . . .