prakash Posted December 14, 2007 Share Posted December 14, 2007 I have a sql query like select table1.id, table2.id, field1, field2, field3 from table1, table2 where field1=table2.id order by field3 so my question is how to to fetch table1.id or table2.id ? using following is not working at all $data1=$each_row['table1.id']; $data2=$each_row['table2.id']; so how can I get the same value above?? Quote Link to comment Share on other sites More sharing options...
hostfreak Posted December 14, 2007 Share Posted December 14, 2007 Maybe something like: select table1.id as table1_id, table2.id as table2_id, field1, field2, field3 from table1, table2 where program_id=table2.id order by field3 Then: $data1=$each_row['table1_id']; $data2=$each_row['table2_id']; Edit: On a side note, please tell me you edited your code down for simplicity of display. Otherwise, don't expect what you currently have to work at all. Quote Link to comment Share on other sites More sharing options...
prakash Posted December 14, 2007 Author Share Posted December 14, 2007 Maybe something like: select table1.id as table1_id, table2.id as table2_id, field1, field2, field3 from table1, table2 where program_id=table2.id order by field3 Then: $data1=$each_row['table1_id']; $data2=$each_row['table2_id']; Edit: On a side note, please tell me you edited your code down for simplicity of display. Otherwise, don't expect what you currently have to work at all. this is not working while checking through phpmyadmin it output error like "Unknown column 'table2_id' in 'where clause' " any help? Quote Link to comment Share on other sites More sharing options...
hostfreak Posted December 14, 2007 Share Posted December 14, 2007 Are you sure all the spelling in your query is correct? Does table2 exist? Edit: Try: select table1.id as table1_id, table2.id as table2_id, field1, field2, field3 from table1, table2 where program_id=table2_id order by field3 Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 14, 2007 Share Posted December 14, 2007 Try SELECT table1.id AS table1_id, table2.id AS table2_id [...] Then you can access it like table1_id and table2_id. Quote Link to comment Share on other sites More sharing options...
hostfreak Posted December 14, 2007 Share Posted December 14, 2007 Daniel0, I'm pretty sure I already suggested that. The only difference is the capitalization of the mysql statements; which I believe makes no difference? Quote Link to comment Share on other sites More sharing options...
prakash Posted December 14, 2007 Author Share Posted December 14, 2007 Try SELECT table1.id AS table1_id, table2.id AS table2_id [...] Then you can access it like table1_id and table2_id. it lists fileds like table1_id and table2_id without where clause but when where clause is defined it output above error Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 14, 2007 Share Posted December 14, 2007 I was just being lazy, you need to finish the query. The [...] signified something that I removed. Putting the rest in: SELECT table1.id AS table1_id, table2.id AS table2_id, field1, field2, field3 FROM table1, table2 WHERE field1=table2.id ORDER BY field3 Quote Link to comment Share on other sites More sharing options...
prakash Posted December 14, 2007 Author Share Posted December 14, 2007 I was just being lazy, you need to finish the query. The [...] signified something that I removed. Putting the rest in: SELECT table1.id AS table1_id, table2.id AS table2_id, field1, field2, field3 FROM table1, table2 WHERE field1=table2.id ORDER BY field3 there is just the differences of AS being capitalize and this is not working at all any suggestion? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 14, 2007 Share Posted December 14, 2007 No, it's just easier to read. You can do it like as, As, aS or AS. It's case insensitive. Edit: Daniel0, I'm pretty sure I already suggested that. The only difference is the capitalization of the mysql statements; which I believe makes no difference? Oh, I'm sorry. I didn't notice. I'm not sure if you can do this though: from table1, table2 where Quote Link to comment Share on other sites More sharing options...
prakash Posted December 14, 2007 Author Share Posted December 14, 2007 No, it's just easier to read. You can do it like as, As, aS or AS. It's case insensitive. Edit: Daniel0, I'm pretty sure I already suggested that. The only difference is the capitalization of the mysql statements; which I believe makes no difference? Oh, I'm sorry. I didn't notice. I'm not sure if you can do this though: from table1, table2 where read the 7th post Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.