Note that there are some explanatory texts on larger screens.

plurals
  1. POPDO query inside PHP function
    primarykey
    data
    text
    <p>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:</p> <pre><code>function get_topics($array) { $top = 20; $base = 0; foreach ($array as $key =&gt; $value) { $getData = $dbc-&gt;prepare('SELECT * FROM topics WHERE id = :id LIMIT 1');  $getData-&gt;execute(array(':id' =&gt; $value)); while($row = $getData-&gt;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 '&lt;div class="topic_div"&gt;&lt;img src="' . $topic_img . '" width="80"&gt;&lt;br /&gt;&lt;span style="font-size:10pt;"&gt;' . $name . '&lt;/span&gt;&lt;/div&gt;'; } if (++$base == $top) break; } } echo get_topics($some_array); </code></pre> <p>But all I get is an error telling this: "<strong>Parse error: syntax error, unexpected T_VARIABLE in /home/......</strong>", and it says that the problem is on this line:</p> <pre><code>$getData-&gt;execute(array(':id' =&gt; $value)); </code></pre> <p>What can I be doing wrong?</p> <p><strong>EDIT</strong></p> <p>I deleted some code and the code is running fine when this is remaining:</p> <pre><code>function get_topics($array) { foreach ($array as $key =&gt; $value) { echo $value; } } echo get_topics($user_likes_array); </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload