Monday, December 1, 2008

Get content in 2 fields with the same field name in two mysql tables using php

Following code will help you understand how to get values from 2 mysql tables where there are same field names in each table.
This problem usually occurs when you join two tables together.

Example: lets take two tables

Table1
id
s_name
age


Table2
id
p_name
age


In above two tables there is a id field in both.

$query = "select * from Table1, Table2"

The thing in here is not to use mysql_fetch_assoc(), use mysql_fetch_array()

$result = mysql_query($query);
while($data = mysql_fetch_array($result))
{
when you loop through to get the values just remember it will be formatted like following.

}

Table1 id => $data[0]
Table1 s_name = > $data[1]
Table1 age => $data[2]

Table2 id = > $data[3]
Table2 p_name => $data[4]
Table2 age = > $data[5]


Get Free Sinhala IT Learning Videos Kuppiya.com


Get custom programming done at GetAFreelancer.com!

No comments:

Post a Comment