0
<?php
      $assignmentsTable = $ssubjectnamewithoutspace.'_assignments';

        try {
            $stmt = $db->query('SELECT contentID, contentTitle,contentAuthor, contentLink FROM .$assignmentsTable ORDER BY contentID ASC');
            while($row = $stmt->fetch()){

                    echo '<tr><td><a href="'.$row['contentLink'].'">'.$row['contentTitle'].'</a></td></tr>';            


            }

        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    ?>

I get this error

SQLSTATE[42S02]: Base table or view not found 1146 Table 'epiz_5453564654654_newdata.$assignmentsTable' doesn't exist

But table exist in database and when I replace $assignmentsTable with table name it works...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    You have a `.`. in `FROM .$assignmentsTable` which shouldn't be there. – Nigel Ren Sep 26 '18 at 09:21
  • 1
    Welcome. Also, `$assignmentsTable` will not be parsed/replaced since you're using single quotes `'` in `$stmt = $db->query('SELECT contentID,.....` – brombeer Sep 26 '18 at 09:25
  • Thanks @kerbholz – Praveen Kumar Sep 26 '18 at 09:42
  • Your answer helped – Praveen Kumar Sep 26 '18 at 09:42
  • That's why it's bad practice to put vars straight into strings. – Daniel W. Sep 26 '18 at 12:32
  • Hi @Brian, just a quick heads-up. I believe the Review Queue ask that edits are not made to recently closed questions by third parties unless the edit would stand a good chance of reopening the question. This is because edits during a five-day period are reported to the Reopen Queue, so small edits will deny the OP of a chance to have a substantive edit seen by re-openers. However, if you wish to make edits to closed questions that are over five days old, I believe there is no objection (and FWIW, I'd be in support for educational reasons). – halfer Sep 26 '18 at 12:43

1 Answers1

1

$assignmentsTable is not evaluated in your query.

Try :

$stmt = $db->query('SELECT contentID, contentTitle,contentAuthor, contentLink FROM `'.$assignmentsTable.'` ORDER BY contentID ASC');
niiwig
  • 150
  • 4