DannyCruzeira
DannyCruzeira

Reputation: 574

PDO query inside PHP function

I'm making this function where a part of an array should be read, and each value holds a number with which I want to perform a PDO query. This is my following code:

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);

But all I get is an error telling this: "Parse error: syntax error, unexpected T_VARIABLE in /home/......", and it says that the problem is on this line:

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

What can I be doing wrong?

EDIT

I deleted some code and the code is running fine when this is remaining:

function get_topics($array) {


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

echo $value;

  }
}

echo get_topics($user_likes_array);

So it's not that $value is empty, the problem seems to be in the line I mentioned in the beginning, since when I move everything below that line, the error message does not change, but it does change when I move that specific line.

Upvotes: 1

Views: 166

Answers (2)

david strachan
david strachan

Reputation: 7228

foreach ($array as $key => $value) assigns the current element's key to the $value variable on each iteration.

Try

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

Upvotes: 1

Barkermn01
Barkermn01

Reputation: 6832

The code you have posted is correct are you sure your in the right file.

I have just coped your code and sorted out the layout and ran it though my PHP Testing batch and it is correct

Output from testing batch

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 . . .

Code i used

<?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);

if there was an error you would have seen it before the pause command is sent to CMD it would have shown the error

Proof My Tester Works for this i just commented out // the $some_array = array() here is the result

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 . . .

Upvotes: 0

Related Questions