Re: PDO Prevent duplicate field names?
| From: | Matijn Woudt | Date: | Mon, 02 Jul 2012 22:34:46 +0000 |
| Subject: | Re: PDO Prevent duplicate field names? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-318377@lists.php.net to get a copy of this message | ||
On Tue, Jul 3, 2012 at 12:25 AM, Scott Baker <bakers@canbytel.com> wrote:
> $sql = "SELECT First, Last, Age, 'Foobar' AS Last;";
>
> This is a simplified example of a SQL query where we're returning two
> fields with the same name (Last). When I do a fetch_assoc with this
> query I only get three fields, as the second "Last" field over writes
> the first one.
>
> I was hoping there was some method with PDO that would detect that and
> throw a warning. Maybe some sort of "strict mode" that would tell me I'm
> doing something stupid. Is there a way to catch this before it bites me?
>
> It already bit me, but moving forward it'd be nice if PHP saw that
> before I spent an hour debugging it again.
>
> - Scott
>
Why the #### would you want to return 2 columns with the same name?
To be short, there's no such function, so you have to:
1) Rename one of the columns
2) or, use fetch_row with numerical indexes instead of fetch_assoc.
- Matijn