Display Results From Mysql Database

i am messing around with a couple scripts and am having a little issues when it comes to joins

[code] $result = mysql_query("SELECT p.name, m.name, m.dose FROM patients p, meds m WHERE p.med_id = m.med_id ") or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("Name: %s Med %s
", $row[‘p.name’], $row[‘m.name’]);[/code]

now what i want it to do is print the patients name and the corisponding pill

I get :
Name: Med:
Name: Med:
Name: Med:
Name: Med:

now what i did notice is that when i change the
$row[‘p.name’] to $row[0] it returns the Patients name
$row[‘m.name’] to $row[1] it returns the Meds name

[code] $result = mysql_query("SELECT p.name, m.name, m.dose FROM patients p, meds m WHERE p.med_id = m.med_id ") or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("Name: %s Med: %s
", $row[0], $row[1]);[/code]

I Get which is the result it is suppose to return in the first script
Name: Peggy Med: PillA
Name: Bert Med: PillB
Name: Lisa Med: PillD
Name: Jim Med: PillF

but that is no help since this query is going to be more complex with more fields from both the patient and med so i need to be able to select field name

thanks in advance

Hershal

mysql_fetch_array()'s second parameter is MYSQL_BOTH, meaning it gives two arrays (merged into your variable): one’s associative, one’s numeric. Hence why $row[0] and $row[1] contain values. The reason $row[‘m.name’] and $row[‘p.name’] don’t have values, is because you (probably) have the indexes incorrect (they’re probably not called ‘m.name’ and ‘p.name’). You can check that using print_r(). You could use the following for your query:

SELECT p.name AS patient_name, m.name AS med_name FROM patients p, meds m

This will assign the indexes as ‘patient_name’ and ‘med_name’ respectively.

Sponsor our Newsletter | Privacy Policy | Terms of Service