[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 TABLE Main</FONT><FONT =
color=3D#007700>(
<BR></FONT><FONT color=3D#0000bb>id VARCHAR</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>10</FONT><FONT =
color=3D#007700>) </FONT><FONT =
color=3D#0000bb>NOT NULL</FONT><FONT color=3D#007700>,
<BR></FONT><FONT color=3D#0000bb>Name VARCHAR</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>100</FONT><FONT =
color=3D#007700>),
<BR></FONT><FONT color=3D#0000bb>PRIMARY KEY </FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>id</FONT><FONT =
color=3D#007700>));
<BR>
<BR></FONT><FONT =
color=3D#0000bb>CREATE TABLE Phone</FONT><FONT =
color=3D#007700>(
<BR></FONT><FONT color=3D#0000bb>id VARCHAR</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>10</FONT><FONT =
color=3D#007700>),
<BR></FONT><FONT color=3D#0000bb>phone VARCHAR</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>15</FONT><FONT =
color=3D#007700>),
<BR></FONT><FONT color=3D#0000bb>FOREIGN KEY </FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>id</FONT><FONT =
color=3D#007700>) </FONT><FONT =
color=3D#0000bb>REFERENCES Main </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><?
<BR> $USERNAME </FONT><FONT =
color=3D#007700>=3D </FONT><FONT color=3D#dd0000>'root'</FONT><FONT =
color=3D#007700>;
<BR> </FONT><FONT =
color=3D#0000bb>$PASSWORD </FONT><FONT =
color=3D#007700>=3D </FONT><FONT =
color=3D#dd0000>'passme'</FONT><FONT color=3D#007700>;
<BR>
<BR> </FONT><FONT =
color=3D#0000bb>$DATABASE </FONT><FONT =
color=3D#007700>=3D </FONT><FONT =
color=3D#dd0000>'imthiaz'</FONT><FONT color=3D#007700>;
<BR> </FONT><FONT =
color=3D#0000bb>$TABLE_NAME </FONT><FONT =
color=3D#007700>=3D </FONT><FONT =
color=3D#dd0000>'Phone'</FONT><FONT color=3D#007700>;
<BR>
<BR> </FONT><FONT =
color=3D#0000bb>mysql_connect</FONT><FONT color=3D#007700>(</FONT><FONT =
color=3D#dd0000>'localhost'</FONT><FONT =
color=3D#007700>, </FONT><FONT =
color=3D#0000bb>$USERNAME</FONT><FONT =
color=3D#007700>, </FONT><FONT =
color=3D#0000bb>$PASSWORD</FONT><FONT color=3D#007700>)
<BR> or die (</FONT><FONT =
color=3D#dd0000>"Could not connect"</FONT><FONT color=3D#007700>);
<BR>
<BR> </FONT><FONT =
color=3D#0000bb>$result </FONT><FONT =
color=3D#007700>=3D </FONT><FONT =
color=3D#0000bb>mysql_query</FONT><FONT color=3D#007700>(</FONT><FONT =
color=3D#dd0000>"SHOW FIELDS FROM $DATABASE.$TABLE_NAME"</=
FONT><FONT color=3D#007700>);
<BR>
<BR> </FONT><FONT =
color=3D#0000bb>$i </FONT><FONT =
color=3D#007700>=3D </FONT><FONT color=3D#0000bb>0</FONT><FONT =
color=3D#007700>;
<BR>
<BR> while (</FONT><FONT =
color=3D#0000bb>$row </FONT><FONT =
color=3D#007700>=3D </FONT><FONT =
color=3D#0000bb>mysql_fetch_array</FONT><FONT =
color=3D#007700>(</FONT><FONT color=3D#0000bb>$result</FONT><FONT =
color=3D#007700>))
<BR> {
<BR> echo </FONT><FON=
T color=3D#0000bb>$row</FONT><FONT color=3D#007700>[</FONT><FONT =
color=3D#dd0000>'Field'</FONT><FONT =
color=3D#007700>] . </FONT><FONT color=3D#dd0000>' =
' </FONT><FONT color=3D#007700>. </FONT><FONT =
color=3D#0000bb>$row</FONT><FONT color=3D#007700>[</FONT><FONT =
color=3D#dd0000>'Type'</FONT><FONT color=3D#007700>];
<BR> }
<BR></FONT><FONT color=3D#0000bb>?>
<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: