PHP: Hypertext Preprocessor (original) (raw)

odbc_primarykeys

(PHP 4, PHP 5, PHP 7, PHP 8)

odbc_primarykeys — Gets the primary keys for a table

Description

odbc_primarykeys(
Odbc\Connection $odbc,
?string $catalog,
string $schema,
string $table
): Odbc\Result|false

Parameters

odbc

The ODBC connection object, see odbc_connect() for details.

catalog

The catalog ('qualifier' in ODBC 2 parlance).

schema

The schema ('owner' in ODBC 2 parlance).

table

Return Values

Returns an ODBC result object or [false](reserved.constants.php#constant.false) on failure.

The result set has the following columns:

Drivers can report additional columns.

The result set is ordered by TABLE_CAT, TABLE_SCHEM,TABLE_NAME and KEY_SEQ.

Changelog

Version Description
8.4.0 odbc expects an Odbc\Connection instance now; previously, a resource was expected.
8.4.0 This function returns an Odbc\Result instance now; previously, a resource was returned.

Examples

Example #1 List primary Keys of a Column

<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>o</mi><mi>n</mi><mi>n</mi><mo>=</mo><mi>o</mi><mi>d</mi><mi>b</mi><msub><mi>c</mi><mi>c</mi></msub><mi>o</mi><mi>n</mi><mi>n</mi><mi>e</mi><mi>c</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">conn = odbc_connect(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">co</span><span class="mord mathnormal">nn</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">o</span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord"><span class="mord mathnormal">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">c</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">o</span><span class="mord mathnormal">nn</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>dsn, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mi>s</mi><mi>e</mi><mi>r</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">user, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.02778em;">ser</span><span class="mpunct">,</span></span></span></span>pass); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi><mi>r</mi><mi>i</mi><mi>m</mi><mi>a</mi><mi>r</mi><mi>y</mi><mi>k</mi><mi>e</mi><mi>y</mi><mi>s</mi><mo>=</mo><mi>o</mi><mi>d</mi><mi>b</mi><msub><mi>c</mi><mi>p</mi></msub><mi>r</mi><mi>i</mi><mi>m</mi><mi>a</mi><mi>r</mi><mi>y</mi><mi>k</mi><mi>e</mi><mi>y</mi><mi>s</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">primarykeys = odbc_primarykeys(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">p</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">ima</span><span class="mord mathnormal" style="margin-right:0.03588em;">ry</span><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="mord mathnormal">eys</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">o</span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord"><span class="mord mathnormal">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">p</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">ima</span><span class="mord mathnormal" style="margin-right:0.03588em;">ry</span><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="mord mathnormal">eys</span><span class="mopen">(</span></span></span></span>conn, 'TutorialDB', 'dbo', 'TEST'); while (($row = odbc_fetch_array($primarykeys))) { print_r($row); break; // further rows omitted for brevity } ?>

The above example will output something similar to:

Array ( [TABLE_CAT] => TutorialDB [TABLE_SCHEM] => dbo [TABLE_NAME] => TEST [COLUMN_NAME] => id [KEY_SEQ] => 1 [PK_NAME] => PK__TEST__3213E83FE141F843 )

See Also

Found A Problem?

Rio Bautista (RRIT)

21 years ago

`Sample to get the primary keys of an MSSQL table:

$cn = odbc_connect( "DSN", "sa", "pwd"); rs=odbcprimarykeys(rs = odbc_primarykeys( rs=odbcprimarykeys(cn, "database", "dbo", "table_name");

odbc_result_all($rs);

`

dan dot scott at ca dot ibm dot com

20 years ago

`Responding to devendra_joshi:

In DB2 Universal Database for Linux, UNIX, and Windows the catalog views are accessed through the SYSCAT schema, not the SYSIBM schema -- so you should be issuing "SELECT * FROM SYSCAT.KEYCOLUSE" to list all of the columns that participate in a given key constraint.

A complete list of the catalog views for DB2 can be referenced at http://publib.boulder.ibm.com/infocenter/db2help/ by searching for 'catalog views' and selecting the top hit.

`

devendra_joshi at hotmail dot com

21 years ago

`I want a list of primary keys of a table in db2

by using
'select * from SYSIBM.SYSKEYCOLUSE ' query i am getting the result on CLP
but when i am writing it in PHP as follows it returns 0 ROWS. mstmt="select∗fromSYSIBM.SYSKEYCOLUSE";<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mo>=</mo><mi>o</mi><mi>d</mi><mi>b</mi><msub><mi>c</mi><mi>e</mi></msub><mi>x</mi><mi>e</mi><mi>c</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">b=odbcexec(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">b</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">d</span><spanclass="mordmathnormal">b</span><spanclass="mord"><spanclass="mordmathnormal">c</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight">e</span></span></span></span><spanclass="vlist−s">​</span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">x</span><spanclass="mordmathnormal">ec</span><spanclass="mopen">(</span></span></span></span>conn,mstmt="select * from SYSIBM.SYSKEYCOLUSE"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mo>=</mo><mi>o</mi><mi>d</mi><mi>b</mi><msub><mi>c</mi><mi>e</mi></msub><mi>x</mi><mi>e</mi><mi>c</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">b=odbc_exec(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">o</span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord"><span class="mord mathnormal">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">e</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">x</span><span class="mord mathnormal">ec</span><span class="mopen">(</span></span></span></span>conn,mstmt="selectfromSYSIBM.SYSKEYCOLUSE";<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mo>=</mo><mi>o</mi><mi>d</mi><mi>b</mi><msub><mi>c</mi><mi>e</mi></msub><mi>x</mi><mi>e</mi><mi>c</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">b=odbcexec(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">b</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">d</span><spanclass="mordmathnormal">b</span><spanclass="mord"><spanclass="mordmathnormal">c</span><spanclass="msupsub"><spanclass="vlisttvlistt2"><spanclass="vlistr"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:2.55em;marginleft:0em;marginright:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingresetsize6size3mtight"><spanclass="mordmathnormalmtight">e</span></span></span></span><spanclass="vlists"></span></span><spanclass="vlistr"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">x</span><spanclass="mordmathnormal">ec</span><spanclass="mopen">(</span></span></span></span>conn,mstmt);
echo odbc_result_all($b);

where as if we write this code mstmt="select∗fromSYSIBM.SYSFUNCTIONS";<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mo>=</mo><mi>o</mi><mi>d</mi><mi>b</mi><msub><mi>c</mi><mi>e</mi></msub><mi>x</mi><mi>e</mi><mi>c</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">b=odbcexec(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">b</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">d</span><spanclass="mordmathnormal">b</span><spanclass="mord"><spanclass="mordmathnormal">c</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight">e</span></span></span></span><spanclass="vlist−s">​</span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">x</span><spanclass="mordmathnormal">ec</span><spanclass="mopen">(</span></span></span></span>conn,mstmt="select * from SYSIBM.SYSFUNCTIONS"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mo>=</mo><mi>o</mi><mi>d</mi><mi>b</mi><msub><mi>c</mi><mi>e</mi></msub><mi>x</mi><mi>e</mi><mi>c</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">b=odbc_exec(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">b</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">o</span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord"><span class="mord mathnormal">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">e</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">x</span><span class="mord mathnormal">ec</span><span class="mopen">(</span></span></span></span>conn,mstmt="selectfromSYSIBM.SYSFUNCTIONS";<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mo>=</mo><mi>o</mi><mi>d</mi><mi>b</mi><msub><mi>c</mi><mi>e</mi></msub><mi>x</mi><mi>e</mi><mi>c</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">b=odbcexec(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">b</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">d</span><spanclass="mordmathnormal">b</span><spanclass="mord"><spanclass="mordmathnormal">c</span><spanclass="msupsub"><spanclass="vlisttvlistt2"><spanclass="vlistr"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:2.55em;marginleft:0em;marginright:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingresetsize6size3mtight"><spanclass="mordmathnormalmtight">e</span></span></span></span><spanclass="vlists"></span></span><spanclass="vlistr"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">x</span><spanclass="mordmathnormal">ec</span><spanclass="mopen">(</span></span></span></span>conn,mstmt);
echo odbc_result_all($b);

it returns the correct data.

`

ewilde aht bsmdevelopment dawt com

18 years ago

`I was trying to find the primary keys from an SQLServer database through the ODBC interface. Funnily enough, the odbc_primarykeys function doesn't work with SQLServer (at least not my implementation of it). Fortunately, the sp_keys query is passed through and the answer returned. This code works (providing you know which database you're dealing with, which is a whole 'nother story).

// If this is SQLServer, we need to do a special operation to get the
// primary keys.
//
// Looks like the implementers of the ODBC interface just blew this
// one off, since the database has a query to return the info and the
// info even comes back with the same column names.
if ($DBType == "SQLServer")
KeySel=odbcexec(KeySel = odbc_exec(KeySel=odbcexec(DBConn, "sp_pkeys ".$TableName);

// Otherwise, ask the database through ODBC for the primary key
// names.
else KeySel=odbcprimarykeys(KeySel = odbc_primarykeys(KeySel=odbcprimarykeys(DBConn, $DatabaseName,
DatabaseUser,DatabaseUser, DatabaseUser,TableName);

while ($KeySel && ($KeyRec = odbc_fetch_array($KeySel)))
KeyCol[KeyCol[KeyCol[KeyRec["KEY_SEQ"]] = $KeyRec["COLUMN_NAME"];

`