using prepared statements and fetchAll() to display to a table : <p>I'm trying to get my code SQL Injection safe, I am having trouble converting the pdo data to an array then comparing row data.</p>

<p>I have been reading up on <a href="https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php">how to prevent sql injection</a> as well as <a href="https://www.php.net/manual/en/pdostatement.fetchall.php" rel="nofollow noreferrer">fetchAll() documentation</a> and
<a href="https://phpdelusions.net/pdo_examples/select" rel="nofollow noreferrer">how to handle SELECT statements with pdo</a>.</p>

<p>Here is the relevant code. I believe it prepares the statement, then executes it, then the table is fetched and stored in $data where it is handed to $row, and then it looks up the player column and compares it with the logged in user to get the context based code to run. Where is the issue?</p>

<pre class="lang-php prettyprint-override"><code>
$stmt = $pdo-&gt;prepare("SELECT * FROM userdata");

$stmt-&gt;execute();

$data = $stmt-&gt;fetchAll();

echo "&lt;table border='1'&gt;
&lt;tr&gt;
&lt;th&gt;username&lt;/th&gt;
&lt;th&gt;words&lt;/th&gt;
&lt;/tr&gt;";

while($row = $data)
{
echo $row['player'];
echo "&lt;tr&gt;";
echo "&lt;td&gt;" . $row['player'] . "&lt;/td&gt;";
if($row['player'] == $logedInUsername)
{
    echo "&lt;td&gt;" . $row['words'] . "&lt;a href='edityourword.php?edit=$row[words]'&gt; edit&lt;/a&gt;&lt;/td&gt;";
}
else
{
    echo "&lt;td&gt;" . $row['words'] . "&lt;/td&gt;";
}
echo "&lt;/tr&gt;";
}
echo "&lt;/table&gt;";

</code></pre>

<p>My current error is reoccurring, here is the segment which the while loop keeps printing.</p>

<pre><code>Notice: Undefined index: player on line 41

Notice: Undefined index: player on line 43

Notice: Undefined index: player on line 44

Notice: Undefined index: words on line 50

Notice: Undefined index: player on line 41

Notice: Undefined index: player on line 43

Notice: Undefined index: player on line 44

Notice: Undefined index: words on line 50
</code></pre>
