1

我正在制作这个函数,其中应该读取数组的一部分,并且每个值都包含一个我想要执行 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 是空的,问题似乎出在我一开始提到的那一行,因为当我将所有内容移到该行下方时,错误消息不会改变,但是当我移动该特定行时它确实会改变。

4

2 回答 2

1

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

尝试

$getData->execute(':id',$value);
于 2012-11-24T09:01:34.193 回答
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);

如果在将 pause 命令发送到 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 . . .
于 2012-11-23T23:28:39.473 回答