473,320 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Echo record from referenced table

27
hi;

i have two tables in MySQL with:
1. TABLE(stat) column(c_id, FOREIGN KEY) referencing

2. TABLE (complaint) column (id_complaint, PRIMARY KEY).

TABLE (complaint) also contain a second column (complaint_type).

How can i get PHP to echo second column (complaint type) in TABLE (complaint) when it is referencing the PRIMARY KEY of this table instead(id_complaint). as regards the below mysql query:

[PHP]$sum= ("SELECT c_id, SUM(number) FROM stat
WHERE '$_POST[m_id]' = m_id AND '$_POST[l_id]' = l_id AND '$_POST[y_id]' = y_id
GROUP BY c_id") or die (mysql_error());
$sum_1 = mysql_query($sum) or die (mysql_error());
while ($sum_2 = mysql_fetch_array($sum_1)) {echo "<b>Total:</b>"; echo $sum_2 ['c_id']; echo "&nbsp"; echo $sum_2['SUM(number)']; echo "<br>";}
[/PHP]

the area of concern which echoes the PRIMARY KEY is:
[PHP]echo $sum_2['c_id'];[/PHP]

and if i use:
[PHP]echo $sum_2['complaint_type'];[/PHP]it states that column does not exist which is understandable becos am selecting stat table in my query not complaint.

expecting any response. thanks
Aug 10 '07 #1
4 1692
nathj
938 Expert 512MB
hi;

i have two tables in MySQL with:
1. TABLE(stat) column(c_id, FOREIGN KEY) referencing

2. TABLE (complaint) column (id_complaint, PRIMARY KEY).

TABLE (complaint) also contain a second column (complaint_type).

How can i get PHP to echo second column (complaint type) in TABLE (complaint) when it is referencing the PRIMARY KEY of this table instead(id_complaint). as regards the below mysql query:

[PHP]$sum= ("SELECT c_id, SUM(number) FROM stat
WHERE '$_POST[m_id]' = m_id AND '$_POST[l_id]' = l_id AND '$_POST[y_id]' = y_id
GROUP BY c_id") or die (mysql_error());
$sum_1 = mysql_query($sum) or die (mysql_error());
while ($sum_2 = mysql_fetch_array($sum_1)) {echo "<b>Total:</b>"; echo $sum_2 ['c_id']; echo "&nbsp"; echo $sum_2['SUM(number)']; echo "<br>";}
[/PHP]

the area of concern which echoes the PRIMARY KEY is:
[PHP]echo $sum_2['c_id'];[/PHP]

and if i use:
[PHP]echo $sum_2['complaint_type'];[/PHP]it states that column does not exist which is understandable becos am selecting stat table in my query not complaint.

expecting any response. thanks
Hi,

You can only echo columns that you have selected. So if you select the information in the SQL you will be able to display it on the screen. The area you need to work on is the SQL. Probably a join will do the trick for you.

Expand|Select|Wrap|Line Numbers
  1. select a.c_id, b.complaint_type from stat a left outer join complaint b on b.complaint_id = a.c_id where ...
  2.  
This should do the trick for you, as in the result you will have complaint_type and will therefore be able to display in the browser.

Cheers
nathj
Aug 10 '07 #2
prosad
27
Hi,

You can only echo columns that you have selected. So if you select the information in the SQL you will be able to display it on the screen. The area you need to work on is the SQL. Probably a join will do the trick for you.

Expand|Select|Wrap|Line Numbers
  1. select a.c_id, b.complaint_type from stat a left outer join complaint b on b.complaint_id = a.c_id where ...
  2.  
This should do the trick for you, as in the result you will have complaint_type and will therefore be able to display in the browser.

Cheers
nathj
thnks for the lead, below is what i developed:

[PHP]$sum= ("SELECT a.complaint, SUM(b.number) Number FROM complaint a, stat b
WHERE '$_POST[m_id]' = m_id AND '$_POST[l_id]' = l_id AND '$_POST[y_id]' = y_id AND
a.id_complaint = b.c_id
GROUP BY c_id") or die (mysql_error());
$sum_1 = mysql_query($sum) or die (mysql_error());
echo "<table border = '1'>"; echo "<th>Complaint</th><th>Number</th>";
while ($sum_2 = mysql_fetch_array($sum_1))
{echo "<tr><td>"; echo "<b>"; echo $sum_2 ['complaint']; echo "</td>";
echo "<td>"; echo $sum_2['SUM(b.number)']; echo "</td></tr>"; echo "<br>";}
echo "</table>";[/PHP]
my problem is that if i try to echo $sum_2['SUM(b.number)'] it does not produce anything in browser. though the SQL query produces intended result at the backend with columns 'complaint' and 'SUM(b.number)'.
Aug 13 '07 #3
nathj
938 Expert 512MB
Hi,

The trouble here is again in the SQL.

You need to call the result of sum something in the SQL, so you would have :
Expand|Select|Wrap|Line Numbers
  1. sum(b.field) as field_total
  2.  
Then you could reference the field in the normal way as it would be named in the normal way.

Does that make sense?

Cheers
nathj
Aug 13 '07 #4
prosad
27
Hi,

The trouble here is again in the SQL.

You need to call the result of sum something in the SQL, so you would have :
Expand|Select|Wrap|Line Numbers
  1. sum(b.field) as field_total
  2.  
Then you could reference the field in the normal way as it would be named in the normal way.

Does that make sense?

Cheers
nathj
thanks alot,
was just about to post a reply that have gotten round this problem wen i noticed u had already replied. what i did in the end is this (more simple logic then the former):

[PHP]$sum= ("SELECT complaint, SUM(number) FROM complaint, stat
WHERE '$_POST[m_id]' = m_id AND '$_POST[l_id]' = l_id AND '$_POST[y_id]' = y_id AND
id_complaint = c_id
GROUP BY c_id") or die (mysql_error());
$sum_1 = mysql_query($sum) or die (mysql_error());
echo "<table border = '1'>"; echo "<th>Complaint</th><th>Number</th>";
while ($sum_2 = mysql_fetch_array($sum_1))
{echo "<tr><td>"; echo "<b>"; echo $sum_2 ['complaint']; echo "</td>";
echo "<td>"; echo $sum_2['SUM(number)']; echo "</td></tr>"; echo "<br>";}
echo "</table>";
[/PHP]
Aug 13 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
11
by: Neil | last post by:
I need to temporarily remove the recordset from my form while I run some queries (have locking problems). So I turned Echo off before removing the recordset to avoid getting #Name? in the bound...
1
by: Bernard Dhooghe | last post by:
The documentation (Administrative API Reference, 8.2, PDF format) says page 520: "The rest of the record is dependent upon the record type and the table descriptor record defined for the table."...
19
by: Genalube | last post by:
I have an application that will produce a Word document based on five separate queries this has required that I create a ADODB connection: 'Create connection to current database Dim Conn As...
2
by: webandwe | last post by:
Hi, Once I pressed submit to delete I want to echo, "record deleted". It is not displaying now, can someone tell me why....It code it at the bottom. Thanks
3
prn
by: prn | last post by:
Hi folks, I've got something that's driving me crazy here. If you don't want to read a long explanation, this is not the post for you. My problematic Access app is a DB for keeping track of...
1
by: lionelm | last post by:
Hi there, I have the following tables: ProductionRun: {ProductionRunID(Primary Key), Product(foreign key referencing ProductRecipe table)} ProductRecipe: {Product(primary key), ....} ...
1
by: raddrummer | last post by:
Hi gang, I'm stumped on something that I've been at for a few days and so am seeking help. I'm sending emails to managers to let them know that a performace review is due and attach a custom...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.