0

I want to show data from sql in my php page but I get this error:

"Error: SQLSTATE[42000]: Syntax error or access violation: 156 [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near the keyword 'user'. (SQLExecute[156] at ext\pdo_odbc\odbc_stmt.c:258)"

This is the code I am using and when I check if the odbc:odbc2sqlserver connection it says 'true'.

I searched on other questions but I could not find working answer.

<tr>
        <td>
            <div id="wrapper">
                <?php
                echo "<table style='border: solid 1px black;'>";
                echo "<tr><th>UserID</th><th>Username</th><th>Password</th><th>Beheerder</th></tr>";

                class TableRows extends RecursiveIteratorIterator {
                    function __construct($it) {
                        parent::__construct($it, self::LEAVES_ONLY);
                    }

                    function current() {
                        return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
                    }

                    function beginChildren() {
                        echo "<tr>";
                    }

                    function endChildren() {
                        echo "</tr>" . "\n";
                    }
                }

                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "Taaltrainer";

                try {
                    $conn = new PDO("odbc:odbc2sqlserver");
                    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    $stmt = $conn->prepare("SELECT * FROM user");
                    $stmt->execute();

                    // set the resulting array to associative
                    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
                    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
                        echo $v;
                    }
                }
                catch(PDOException $e) {
                    echo "Error: " . $e->getMessage();
                }
                $conn = null;
                echo "</table>";
                ?>
            </div>
        </td>
    </tr>
Barmar
  • 741,623
  • 53
  • 500
  • 612
Luuk
  • 1
  • 3
    Try `SELECT * FROM [user]`. (Reserved word.) – jarlh Feb 10 '20 at 18:06
  • I did that and now it says I "Base table or view not found" So I guess that my connection not correct is but when I checked the PDO it said that I had an odbc to use. – Luuk Feb 10 '20 at 18:10
  • You can check this for using mssql connection via pdo odbc: https://stackoverflow.com/questions/20163776/connect-php-to-mssql-via-pdo-odbc. And this is the sql query: SELECT * FROM root.user – OO7 Feb 10 '20 at 20:09

0 Answers0