Link to home
Start Free TrialLog in
Avatar of Brodie Krause
Brodie KrauseFlag for United States of America

asked on

Calling MySQL Fields in PHP

Hello,

I'm very new to PHP and MySQL.  I'm trying to troubleshoot a problem on my company's website, where certain fields that should show up are not.  The address doesn't show up, but the city and state do.

In looking at the code, I see where the fields are being called, but none of the field names actually match the field names in the database itself, even those fields which are displaying correctly.

Is there some sort of field-name-aliasing in PHP that I'm missing here?
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

AFAIK, there is no such a system. Maybe the field names are wrapped in constants so you should find somewhere something like define(USEDFIELDNAME, 'databaseFieldName');
but this doesn't seem to be a good practice, nor so usual. At the end of the day, I think this is very strange and if you could post here some code maybe I can help you better...

Cheers
SOLUTION
Avatar of Jagadeesh M
Jagadeesh M
Flag of United States of America 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
Avatar of Brodie Krause

ASKER

jagadeesh_motamarri, thanks!  Your sample code pointed me to the intial query for the MySQL db.

Here's the query:
$sql = "SELECT UID AS id, FirstName AS first, LastName AS last,  Location AS loc1, CONCAT(Address,Address2) AS add1, City AS city1, State AS state1, Postal AS zip1, Phone AS phone1, 2Location AS loc2, CONCAT(2Address,2Address2) AS add2, 2City AS city2, 2State AS state2, 2Postal AS zip2, 2Phone as phone2 FROM person ";

Open in new window


It seems this "AS" modifier is translating from the actual DB field name to what the developer wrote the code to call.  However, here's the section of code I'm looking at:
<?php
if($person['Location1'] != '') 
        { 
        $locfields = array('Location','add','city','state','zip','phone');
	for($i=1;$i<=4;$i++) 
                {
		foreach($locfields as $field) 
                        { 
			if(isset($person[$field.$i]) && $person[$field.$i] != '') 
                                { 
				echo $person[$field.$i]."\n<br>\n";
			        }
		        }
	echo "\n<br>\n";
	        }
        }
?>

Open in new window


So strangely, the names being called here don't even match what is being defined in that Query (they lack the numerical identifier).  But, in this case, the Zip code is displaying properly.  If I remove this zip reference, the zip code disappears off the served up page, so I know I'm looking in the right spot, I just fail to see the connection between these references and the db.
This seems to be the right code.

Observe the variable i in the for loop -

for($i=1;$i<=4;$i++) 

Open in new window


and this is what is being concatenated with the fields in the below line -

if(isset($person[$field.$i]) && $person[$field.$i] != '')

Open in new window


so each field is accessed as $field.$i
Hi... I maybe wrong but... I do not see at any place, how is the $SQL executed, in other words the call of function mysql_query....

That function loads the corresponding resource where information it's going to be saved...
Usually, information is loaded into an array with something as

$fields = mysql_query($Query1) or die ("Database error");

Open in new window


afterwards, you retrieve each row data using the function mysql_fetch_array

$row=mysql_fetch_array($fields)

Open in new window


You should check for some variation of that.


Regards
Goofy
SOLUTION
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
ASKER CERTIFIED SOLUTION
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
Ever notice how the Venn diagram of "clever" seems to overlap "stupid"? ;-)

Oh yes.   I guess I shouldn't complain.  These events make money for you and me and Cd&.  It seems that the clever ones think no one else will be able to maintain the site that way.

So boys and girls, just keep on with the clever coding.  When it stops working, they will hire one of us older coders to fix it.  Unless it's a Smarty template or Xoops.  Those are real close to being cleverly Wrong even when the are right.
@DaveBaldwin: Right On!
@DaveBaldwin:   I had a feeling that was the case, regarding the clever developer, but lacked the foundational knowledge to throw that stone!  From the little bit I've gleaned from the code, my reaction has consistently been, "It's got to be simpler than that!"  After all, why would anyone rely so heavily on PHP if it is really this cumbersome!  :-)  I just stepped into this job a couple months ago, and when the issues with this site were brought to my attention, I was told the developer wanted an additional $3k to fix the problems, or $5k to rewrite the site "using updated standards and practices".   Needless to say, I'm now very motivated to learn this stuff to break our dependence on guys like this.

@Ray_Paseur:   Thanks for the tips and the resource, I've just placed the order and will be checking it out ASAP.   From what I can tell, you're dead on that the site was not written using best practices.
Clever code and incompetent code are the two things that keep me in business.  There are some things I won't work on like Smarty and Xoops because they are frameworks that are too complicated to begin with.  But most things can done fairly simply even with a pretty fair amount of detail.  Good luck on your project.
Thanks for the points, and best of luck with it! ~Ray