Link to home
Start Free TrialLog in
Avatar of breeze351
breeze351

asked on

Echo values after a query in php

I need to echo the values back from a query.
$Sql_String = "SELECT 
	survey_data.*,
	pers.FNAME,
	pers.LNAME,
	pers.PHONE1
FROM
	survey_data
JOIN pers ON survey_data.TID = pers.TID and survey_data.PNID = pers.PNID
WHERE
`SEQ` = 'NY1050000350' and `STOREKEY` = '1001'";
$survey_data = $conn->query($Sql_String);
$row = $survey_data->fetch_array();
echo $row['survey_data.ADDRESS']."<br>";
echo $row['pers.FNAME']."<br>";
echo $row['pers.LNAME']."<br>";

Open in new window


The query works but I'm getting the following error:
Notice: Undefined index: survey_data.ADDRESS in /home/mrbreez1/public_html/test/joinq.php on line 36
Notice: Undefined index: pers.FNAME in /home/mrbreez1/public_html/test/joinq.php on line 38
Notice: Undefined index: pers.LNAME in /home/mrbreez1/public_html/test/joinq.php on line 39

What did I do wrong?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Did the query run correctly?  You can test the value in $survey_data to see if it is a query results set or FALSE.  If it's a results set, are there any rows in the results set?  Rows would be present if the JOIN and WHERE clause was satisfied.  But if the query worked and JOIN / WHERE was not satisfied, you would get a results set, but it would be empty.  You can use var_dump($survey_data) to see if you have a results set.  You can use var_dump($row) to see the return values from fetch_array().  This will tell you what keys to use to get data from the $row array.  The column names and array keys are case-sensitive, so FNAME != FName.
Avatar of hielo
>>echo $row['survey_data.ADDRESS']."<br>";
>>echo $row['pers.FNAME']."<br>";
>>echo $row['pers.LNAME']."<br>";
Use only the field names, not the table prefix:
echo $row['ADDRESS']."<br>";
echo $row['FNAME']."<br>";
echo $row['LNAME']."<br>";

Open in new window

Avatar of breeze351
breeze351

ASKER

What if you have the same variable name in 2 different tables?  In this case the "comp" (company table) has a "PHONE1" and so does the "pers" (person table).  They can and are often different.
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, the last language I was using it wanted the table.field