0

I'm working on a DB2 table on as/400. I'm trying to run a basic query against it but the table name contains a . in it. For example: my.table is the table name.

The problem is that PDO believes that I'm specifying a database name my with my table name table. Hence the prepared statement is failing and saying no such table table exists in database my. Changing the table name is not an option.

What is the best way to handle a period in the table name? I've tried escaping the period but haven't had any success.

Here's some example code of my problem:

$sql= "select * from '[THE.TABLE]'";
        try {
            $statement = $this->db->query($sql);
            $results = $statement->execute();
            foreach ($results as $result) {
                print_r($result);
            }
            exit;
        }
        catch (\Exception $e)
        {
            //log issue and other stuff
        }

The application is running in Zend Framework 2.

Knight
  • 576
  • 7
  • 19
  • Try putting backticks around table name: `\`my.table\`` – aynber Dec 04 '17 at 19:26
  • 1
    Possible duplicate of [When to use single quotes, double quotes, and backticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks-in-mysql) – aynber Dec 04 '17 at 19:26
  • I tried the back ticks as well. It didn't work. – Knight Dec 04 '17 at 19:38
  • IMHO, I don't think this is a duplicate because I'm not using MySQL but rather DB2 via ODBC. I'm fairly new to this type of Database access but I've tried the quotes and backticks already without success. – Knight Dec 04 '17 at 19:40
  • PDO don't know nothing about table names and don't care for them. What error do you get? – Your Common Sense Dec 04 '17 at 19:47
  • I've solved it thanks to @aynber referencing the other question. – Knight Dec 04 '17 at 19:53

1 Answers1

1

I stand corrected. As aynber mentioned in a comment, the solution was to use double quotes escaped correctly. The answer could be found in the question directed towards MySQL.

Knight
  • 576
  • 7
  • 19