ai dee
ai dee

Reputation: 47

PHP PDO MYSQL/MARIADB How to query a view? (view not found error)

I have a view in a MariaDB and a simple PDO query to select all data.

Working with regular tables, there are no issues at all. The only issue I have is that as soon as I use a VIEW, I get the error that the view does not exist.

[826]::sqlstate[42s02]: base table or view not found: 1146 table 'kafexxxx_diner.reservationsxmonth' doesn't exist

::select reservationsxmonth.property,reservationsxmonth.period,reservationsxmonth.total from reservationsxmonth limit 0, 10000

It doesn't look like a rights issue as the statement performs fine in phpMyAdmin.

The question is, can I use PDO to access views, and how to do it? Does the PDO connection require certain options?

     $pdo='mysql:host=127.0.0.1;dbname=kafexxxx_diner;port=3306;charset=utf8';
     $this->connection = new PDO($pdo, $this->userid, $this->password, $options);
     $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

     

Upvotes: 1

Views: 983

Answers (1)

nbk
nbk

Reputation: 49375

This works without a problem, so you should check for typos or check in your database, if the view was created

<?php
$stmt = $pdo->query("SELECT * FROM view_name");
while ($row = $stmt->fetch()) {
    echo $row['name']."<br />\n";
}

See example

Upvotes: 1

Related Questions