无法使用 MSSQL 在 PDO 中引用表名 [英] Unable to quote table name in PDO with MSSQL

查看:50
本文介绍了无法使用 MSSQL 在 PDO 中引用表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用某人的数据库来开发一个游戏,该游戏可悲的是有一个名为User"或 [dbo][User] 的表,并且无法重命名.现在,我需要在 PHP 中使用 PDO 以及在使用此查询时访问它:

I have to work with somebody's database for a game which sadly has a table named "User" or [dbo][User] and this can not be renamed. Now, I need to access this using PDO in PHP and when I use this query:

$query = "SELECT UserId AS INTUSERID FROM dbo.User WHERE YahooId = 'abcdef'";

它失败了,因为用户"是那里的保留关键字,因此没有获取任何内容.从 MS SQL Server 我可以这样做:

it fails, as in nothing is fetched since "User" is a reserved keyword there. From the MS SQL Server I can do this as:

SELECT UserId AS INTUSERID FROM [GameName].[dbo].[User] WHERE YahooId = 'abcdef'

它的工作原理.我应该如何在 PHP 中准备我的查询以使其执行?我试图在表名周围加上单引号,但这没有效果.什么是正确的使用方法

and it works. How should I prepare my query in PHP to make this execute? I have tried to put single quotes around table name but that has no effect. What is the correct way to use

[GameName].[dbo].[User] 

来自 PHP PDO 作为表名?

from PHP PDO as the table name ?

更新:这就是我的连接方式:

try{
    $conn = new PDO("xxx.rds.amazonaws.com,1150;Database=xyz","admin","password");
    } catch(PDOException $e){
        echo "Connection failed: " . $e->getMessage();
    }

推荐答案

在评论中的进一步讨论中,我提供了这个以便问题有答案.我认为方括号在用于定义查询的 php 字符串中可以正常工作,因此我在自己的代码上对其进行了测试,该代码使用 PDO 连接到 MS-SQL.

Further to the discussion in the comments I am providing this so that the question has an answer. I thought square brackets would work fine within the php string used to define the query so I tested it out on my own code that connects to MS-SQL using PDO.

所以下面应该工作...

So the following should work...

$query = "SELECT UserId AS INTUSERID FROM dbo.[User] WHERE YahooId = 'abcdef'";

附注 - 如果您的 YahooId 来自可以被用户操纵的来源(例如 $_GET),您应该研究 Prepared Statements with PDO...

Side note - If your YahooId ever comes from a source that can be manipulated by a user (such as $_GET) you should research Prepared Statements with PDO...

$query = "SELECT UserId AS INTUSERID FROM dbo.[User] WHERE YahooId = ?;";

$statement= $db->prepare($query);

$statement->execute(array($userinput));

这篇关于无法使用 MSSQL 在 PDO 中引用表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆