简体   繁体   English

是否可以使用PHP从连接表中引用类似的SQL列名

[英]Is it possible to reference similar SQL column names from joined tables using PHP

I have a few tables joined together, but several of the columns have similar names. 我有几个表连接在一起,但有几个列有相似的名称。 I'm using php to fetch those values, but I am unable to use the sql identifier in php to pick the correct column... ie in SQL I have two tables Register and Jurisdict. 我正在使用php来获取这些值,但是我无法在php中使用sql标识符来选择正确的列...即在SQL中我有两个表Register和Jurisdict。 both of them have a column called "name". 它们都有一个名为“name”的列。 in SQL I can reference these columns as such 在SQL中我可以像这样引用这些列

r.name
j.name

and these would give me values from the two different tables. 这些会给我两个不同表中的值。 However in I PHP am not able to use the r. 但是我在PHP中无法使用r。 or j. 或者j。 and anytime I use name it returns which ever table is selected first in the SQL statement. 并且只要我使用name,它就会返回SQL语句中首先选择的表。

Here is my current code 这是我目前的代码

<?php


    $sql = "
SELECT j.rpt_name, r.name 
FROM [SMARTCM] . [dbo] . [REGISTER] r

join [SMARTCM] . [dbo] . [jurisdict] j
on j.UniqueKey = r.FK_JURSDICT_KEY

$stmt = sqlsrv_query( $conn, $sql );

?>

I'm using a while loop to generate the PHP, with this tag to reference the column 我正在使用while循环来生成PHP,使用此标记来引用该列

<?php echo $row['name']; ?>

However it breaks if I try and use the table reference (r.name) like i would i sql 但是,如果我尝试使用表引用(r.name),就像我将sql一样,它会中断

you need to use aliasing feature of SQL query.. 你需要使用SQL查询的别名功能..

<?php


    $sql = "
SELECT j.name as j_name, r.name as r_name 
FROM [SMARTCM] . [dbo] . [REGISTER] r

join [SMARTCM] . [dbo] . [jurisdict] j
on j.UniqueKey = r.FK_JURSDICT_KEY";

$stmt = sqlsrv_query( $conn, $sql );


echo $row['j_name'];

echo $row['r_name'];
?>

You haven't shared the code involved in row retrieval but I presume it's using sqlsrv_fetch_array() . 您还没有共享行检索中涉及的代码,但我认为它使用的是sqlsrv_fetch_array() If you look at the documentation you'll see it has a parameter called $fetchType : 如果查看文档,您会看到它有一个名为$fetchType的参数:

array sqlsrv_fetch_array ( resource $stmt [, int $fetchType [, int $row [, int $offset ]]] ) array sqlsrv_fetch_array(resource $ stmt [,int $ fetchType [,int $ row [,int $ offset]]])

fetchType fetchType

A predefined constant specifying the type of array to return. 预定义常量,指定要返回的数组类型。 Possible values are SQLSRV_FETCH_ASSOC , SQLSRV_FETCH_NUMERIC , and SQLSRV_FETCH_BOTH (the default). 可能的值为SQLSRV_FETCH_ASSOCSQLSRV_FETCH_NUMERICSQLSRV_FETCH_BOTH (默认值)。

... and then the important bit: ......然后是重要的一点:

A fetch type of SQLSRV_FETCH_ASSOC should not be used when consuming a result set with multiple columns of the same name. 在使用具有多个同名列的结果集时,不应使用获取类型的SQLSRV_FETCH_ASSOC

So you have to choose: 所以你必须选择:

  • Have dupe names and retrieve stuff from numeric arrays ( SQLSRV_FETCH_NUMERIC ) 具有欺骗名称并从数字数组中检索内容( SQLSRV_FETCH_NUMERIC

  • Get unique names so you can use associative arrays 获取唯一名称,以便您可以使用关联数组

     SELECT j.name AS j_name, r.name AS r_name 

Last but not least, array contents (or any other PHP variable) should never be a mystery to you. 最后但并非最不重要的是,数组内容(或任何其他PHP变量)永远不应该是你的谜。 Please have a look at var_dump() . 请看一下var_dump()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM