[Ilugc] Help

  • From: imthiazrafiq@xxxxxxx (Imthiaz Rafiq)
  • Date: Mon, 19 Aug 2002 16:27:10 +0530

This is a multi-part message in MIME format.

------=_NextPart_000_000B_01C2479D.44B7A9C0
Content-Type: text/plain;
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hello All

Please help me with this.

I want to know how to find whether a given field in mysql table is a =
reference key or not. If so what is the table it is referencing to. Let =
me show a sample query.



MYSQL:-------------------------------------------------------------------=
-----------
CREATE TABLE Main(
id VARCHAR(10) NOT NULL,
Name VARCHAR(100),
PRIMARY KEY (id));

CREATE TABLE Phone(
id VARCHAR(10),
phone VARCHAR(15),
FOREIGN KEY (id) REFERENCES Main (id));

-------------------------------------------------------------------------=
-----


From above we can see two tables. Phone id field is referenced to main =
id field. If we know the query we can tell easily. I want to read the =
same thru php. like....



PHP:---------------------------------------------------------------------=
---------

<?
    $USERNAME =3D 'root';
    $PASSWORD =3D 'passme';

    $DATABASE =3D 'imthiaz';
    $TABLE_NAME =3D 'Phone';

    mysql_connect('localhost', $USERNAME, $PASSWORD)
    or die ("Could not connect");

    $result =3D mysql_query("SHOW FIELDS FROM $DATABASE.$TABLE_NAME");

    $i =3D 0;

    while ($row =3D mysql_fetch_array($result))=20
    {
        echo $row['Field'] . ' ' . $row['Type'];
    }
?>

-------------------------------------------------------------------------=
-----


Please tell me is there any way we can do this as I did above and it =
didn't give the result what I have expected. Please help me with =
this.....

Thanks in Advance...

------=_NextPart_000_000B_01C2479D.44B7A9C0
Content-Type: text/html;
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2716.2200" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV>
<P><FONT face=3D"verdana, arial, helvetica" size=3D2>Hello =
All<BR><BR>Please help me=20
with this.<BR><BR>I want to know how to find whether a given field in =
mysql=20
table is a reference key or not. If so what is the table it is =
referencing to.=20
Let me show a sample query.<BR><BR></FONT>
<BLOCKQUOTE><PRE><FONT face=3Dverdana,arial,helvetica =
size=3D1>MYSQL:</FONT><HR><CODE><FONT color=3D#000000>
<FONT color=3D#0000bb><BR>CREATE&nbsp;TABLE&nbsp;Main</FONT><FONT =
color=3D#007700>(
<BR></FONT><FONT color=3D#0000bb>id&nbsp;VARCHAR</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>10</FONT><FONT =
color=3D#007700>)&nbsp;</FONT><FONT =
color=3D#0000bb>NOT&nbsp;NULL</FONT><FONT color=3D#007700>,
<BR></FONT><FONT color=3D#0000bb>Name&nbsp;VARCHAR</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>100</FONT><FONT =
color=3D#007700>),
<BR></FONT><FONT color=3D#0000bb>PRIMARY&nbsp;KEY&nbsp;</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>id</FONT><FONT =
color=3D#007700>));
<BR>
<BR></FONT><FONT =
color=3D#0000bb>CREATE&nbsp;TABLE&nbsp;Phone</FONT><FONT =
color=3D#007700>(
<BR></FONT><FONT color=3D#0000bb>id&nbsp;VARCHAR</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>10</FONT><FONT =
color=3D#007700>),
<BR></FONT><FONT color=3D#0000bb>phone&nbsp;VARCHAR</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>15</FONT><FONT =
color=3D#007700>),
<BR></FONT><FONT color=3D#0000bb>FOREIGN&nbsp;KEY&nbsp;</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>id</FONT><FONT =
color=3D#007700>)&nbsp;</FONT><FONT =
color=3D#0000bb>REFERENCES&nbsp;Main&nbsp;</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>id</FONT><FONT =
color=3D#007700>));<BR></FONT><FONT color=3D#0000bb></FONT>
</FONT>
</CODE><HR></PRE></BLOCKQUOTE><FONT face=3D"verdana, arial, helvetica" =
size=3D2><BR><BR>From=20
above we can see two tables. Phone id field is referenced to main id =
field. If=20
we know the query we can tell easily. I want to read the same thru php.=20
like....<BR><BR><BR></FONT>
<BLOCKQUOTE><PRE><FONT face=3Dverdana,arial,helvetica =
size=3D1>PHP:</FONT><HR><CODE><FONT color=3D#000000>

<BR><FONT color=3D#0000bb>&lt;?
<BR>&nbsp;&nbsp;&nbsp;&nbsp;$USERNAME&nbsp;</FONT><FONT =
color=3D#007700>=3D&nbsp;</FONT><FONT color=3D#dd0000>'root'</FONT><FONT =
color=3D#007700>;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT =
color=3D#0000bb>$PASSWORD&nbsp;</FONT><FONT =
color=3D#007700>=3D&nbsp;</FONT><FONT =
color=3D#dd0000>'passme'</FONT><FONT color=3D#007700>;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT =
color=3D#0000bb>$DATABASE&nbsp;</FONT><FONT =
color=3D#007700>=3D&nbsp;</FONT><FONT =
color=3D#dd0000>'imthiaz'</FONT><FONT color=3D#007700>;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT =
color=3D#0000bb>$TABLE_NAME&nbsp;</FONT><FONT =
color=3D#007700>=3D&nbsp;</FONT><FONT =
color=3D#dd0000>'Phone'</FONT><FONT color=3D#007700>;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT =
color=3D#0000bb>mysql_connect</FONT><FONT color=3D#007700>(</FONT><FONT =
color=3D#dd0000>'localhost'</FONT><FONT =
color=3D#007700>,&nbsp;</FONT><FONT =
color=3D#0000bb>$USERNAME</FONT><FONT =
color=3D#007700>,&nbsp;</FONT><FONT =
color=3D#0000bb>$PASSWORD</FONT><FONT color=3D#007700>)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;die&nbsp;(</FONT><FONT =
color=3D#dd0000>"Could not connect"</FONT><FONT color=3D#007700>);
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT =
color=3D#0000bb>$result&nbsp;</FONT><FONT =
color=3D#007700>=3D&nbsp;</FONT><FONT =
color=3D#0000bb>mysql_query</FONT><FONT color=3D#007700>(</FONT><FONT =
color=3D#dd0000>"SHOW&nbsp;FIELDS&nbsp;FROM&nbsp;$DATABASE.$TABLE_NAME"</=
FONT><FONT color=3D#007700>);
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT =
color=3D#0000bb>$i&nbsp;</FONT><FONT =
color=3D#007700>=3D&nbsp;</FONT><FONT color=3D#0000bb>0</FONT><FONT =
color=3D#007700>;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(</FONT><FONT =
color=3D#0000bb>$row&nbsp;</FONT><FONT =
color=3D#007700>=3D&nbsp;</FONT><FONT =
color=3D#0000bb>mysql_fetch_array</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>$result</FONT><FONT =
color=3D#007700>))&nbsp;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</FONT><FON=
T color=3D#0000bb>$row</FONT><FONT color=3D#007700>[</FONT><FONT =
color=3D#dd0000>'Field'</FONT><FONT =
color=3D#007700>]&nbsp;.&nbsp;</FONT><FONT color=3D#dd0000>' =
'&nbsp;</FONT><FONT color=3D#007700>.&nbsp;</FONT><FONT =
color=3D#0000bb>$row</FONT><FONT color=3D#007700>[</FONT><FONT =
color=3D#dd0000>'Type'</FONT><FONT color=3D#007700>];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<BR></FONT><FONT color=3D#0000bb>?&gt;
<BR></FONT></FONT>
</CODE><HR></PRE></BLOCKQUOTE><FONT face=3D"verdana, arial, helvetica" =
size=3D2><BR><BR>Please=20
tell me is there any way we can do this as I did above and it didn't =
give the=20
result what I have expected. Please help me with this.....<BR><BR>Thanks =
in=20
Advance...</FONT></DIV></BODY></HTML>

------=_NextPart_000_000B_01C2479D.44B7A9C0--



Other related posts: