| | |
View Indexed Field in another table
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
I'm having trouble figuring out how to display data from another indexed table. What I have is one table (assets) with a majority of my data in it. I use a simple select statement to display the data. Within that data is a field of "typeid" which is an indexed field to my "assettype" table. With my simple select statement I see only the indexed field "assets.typeid". I want to see the "assettype.type" field from the assettype table.
In a nutshell, i want to change the typeid of "1" to be "printer", but "printer" is in a different table that is indexed by "typeid" number. Here is a snippet, hope it helps.
Further down the script
Where $typeid is echoed, i want to echo $type instead, but it is in a different table that is indexed with $typeid.
In a nutshell, i want to change the typeid of "1" to be "printer", but "printer" is in a different table that is indexed by "typeid" number. Here is a snippet, hope it helps.
PHP Syntax (Toggle Plain Text)
$query = "SELECT * FROM assets WHERE tag = $tag"; $result = mysql_query($query) or die('Sorry, could not find tag requested'); $row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved'); $tag=$row['tag']; $typeid = $row['typeid'];
PHP Syntax (Toggle Plain Text)
echo "<h3><u>Tag#</u><font color=\"#663300\" size=\"2\"> $tag</font></h3>"; echo "<h3><u>Type</u><font color=\"#663300\" size=\"2\"> $typeid</font></h3>\n";
You'll have to query both tables with a WHERE clause and set the indexes equal to each other:
Sorry, I didn't pay attention to your column names, etc. but you get the idea.
PHP Syntax (Toggle Plain Text)
SELECT table1.blah, table2.blahz FROM table1, table2 WHERE table1.tag = table2.tag1;
Sorry, I didn't pay attention to your column names, etc. but you get the idea.
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
Thank you for the reply. I did fail to leave out some information that, as I see, is now pertinant. This page is coming from another page where its using $tag as the id in a $_GET function.
So would i then have to alter my WHERE something like
That didn't work but that's where my mind is going. Or create a $query2 and put them together somehow?
PHP Syntax (Toggle Plain Text)
$tag = $_GET['id']; $query = "SELECT * FROM assets WHERE tag = $tag"; $result = mysql_query($query) or die('Sorry, could not find tag requested'); $row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved'); $tag=$row['tag']; $typeid = $row['typeid'];
So would i then have to alter my WHERE something like
PHP Syntax (Toggle Plain Text)
$query = "SELECT assets.*, assettype.* FROM assets WHERE assets.tag = "$tag" AND assettype.typeid = "$typeid"
That didn't work but that's where my mind is going. Or create a $query2 and put them together somehow?
You need to use a join I think
PHP Syntax (Toggle Plain Text)
$q = @mysql_query("SELECT a.tag, a.field2, at.assettype FROM assets AS a INNER JOIN assettype AS at ON a.assettypeid = at.assettypeid WHERE a.tag = $tag);
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
Ok, this is what I tried.
I have an "or die" on my $result = and recieved an error of "sorry, could not find tag requested"
PHP Syntax (Toggle Plain Text)
$query = @mysql_query("SELECT assets.*, assettype.* FROM assets AS a INNER JOIN assettype AS at ON assets.typeid = assettype.typeid WHERE assets.tag = $tag");
I have an "or die" on my $result = and recieved an error of "sorry, could not find tag requested"
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
No, the tag exsists. If I use the original query The script continues to run and is successful. Only when I try to pull data from 2 different tables in this query is when I'm running into problems.
PHP Syntax (Toggle Plain Text)
$query = "SELECT * FROM assets WHERE tag = $tag";
![]() |
Similar Threads
- comment function (PHP)
- Urgent question about Updates (C#)
Other Threads in the PHP Forum
- Previous Thread: Mp3 Uploader / Player
- Next Thread: paypal sanbox help
| Thread Tools | Search this Thread |
ajax apache api array arrays beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external file files folder form forms forum function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert integration ip java javascript joomla limit link login loop mail malfunction menu method mlm multiple mysql neutrality oop paypal pdf php phpmysql play problem query question radio random recursion regex remote root script search select server sessions sms soap source space sql syntax system table tutorial update upload url validator variable video web xml youtube





