Prismatic Posted May 18, 2006 Share Posted May 18, 2006 Ok, i'm working on my User class and I have an issue. I have a setUserData method which loads all the data from the user from the MySQL DB into variables. The current process by which I do this is below[code] function setUserData($ID){ $QueryStr = "SELECT * FROM users WHERE id = '$ID'"; $Query = mysql_query($QueryStr); $setUserData = mysql_fetch_array($Query); $this->id = $setUserData['id']; $this->username = $setUserData['username']; $this->password = $setUserData['password']; $this->title = $setUserData['title']; $this->signature = $setUserData['signature']; $this->regdate = $setUserData['regdate']; $this->posts = $setUserData['posts']; $this->accesslevel = $setUserData['accesslevel']; $this->status = $setUserData['status']; $this->userstyle = $setUserData['userstyle']; $this->login = $setUserData['login']; $this->msgid = $setUserData['msgid']; $this->style = $setUserData['style']; $this->iconid = $setUserData['iconid']; $this->lastpost = $setUserData['lastpost']; $this->fname = $setUserData['fname']; $this->lname = $setUserData['lname']; $this->pcode = $setUserData['pcode']; $this->country = $setUserData['country']; $this->company = $setUserData['company']; $this->jobtitle = $setUserData['jobtitle']; $this->websiteurl = $setUserData['websiteurl']; $this->pubemail = $setUserData['pubemail']; $this->privemail = $setUserData['privemail']; $this->yahoomesngr = $setUserData['yahoomsnger']; $this->icqid = $setUserData['icqid']; $this->aolid = $setUserData['aolid']; $this->msnid = $setUserData['msnid']; $this->irc = $setUserData['irc']; $this->profiletitle = $setUserData['profiletitle']; $this->gender = $setUserData['gender']; $this->birthday = $setUserData['birthday']; $this->profpic = $setUserData['profpic']; $this->bio = $setUserData['bio']; $this->profadded = $setUserData['profadded']; $this->lastpstdate = $setUserData['lastpstdate']; $this->lastlogindate = $setUserData['lastlogindate']; $this->sig1 = $setUserData['sig1']; $this->sig2 = $setUserData['sig2']; $this->sig3 = $setUserData['sig3']; $this->sig4 = $setUserData['sig4']; $this->sig5 = $setUserData['sig5']; $this->lastip = $setUserData['lastip']; $this->ThreadPages = $setUserData['ThreadPages']; $this->BoardPages = $setUserData['BoardPages']; }[/code]As you can see it's not very nice on the eyes. I was wondering if there was a way to do that all in a for loop? This code:[code] $QueryStr = "SELECT * FROM users WHERE id = '$ID'"; $Query = mysql_query($QueryStr); $setUserData = mysql_fetch_array($Query); for($i=0; $i <= count($setUserData); $i++){ echo "TEST: ". $setUserData[$i] ."<br>"; }[/code]will echo out what each field's value is but not the field name. Any ideas? I need to be able to get the field name AND value :( Link to comment https://forums.phpfreaks.com/topic/9904-getting-field-names-from-query/ Share on other sites More sharing options...
trq Posted May 18, 2006 Share Posted May 18, 2006 [code]$row = mysql_fetch_array($result);foreach($row as $key => $val) { echo "$key = $val<br />";}[/code] Link to comment https://forums.phpfreaks.com/topic/9904-getting-field-names-from-query/#findComment-36816 Share on other sites More sharing options...
Prismatic Posted May 18, 2006 Author Share Posted May 18, 2006 [!--quoteo(post=374875:date=May 18 2006, 12:55 AM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ May 18 2006, 12:55 AM) [snapback]374875[/snapback][/div][div class=\'quotemain\'][!--quotec--][code]$row = mysql_fetch_array($result);foreach($row as $key => $val) { echo "$key = $val<br />";}[/code][/quote] Warning: Invalid argument supplied for foreach() in User.class.php on line 8484: foreach($setUserData as $key => $val) {Any ideas?Edit - Figured it out, the warning appears when an empty array is passed in :) thanks for the help! Link to comment https://forums.phpfreaks.com/topic/9904-getting-field-names-from-query/#findComment-36817 Share on other sites More sharing options...
Barand Posted May 18, 2006 Share Posted May 18, 2006 BEWARE - using foreach() with mysql_fetch_array() will output the variables twice since, by default, it returns both numeric and associative key-value pairs.Better to use mysql_fetch_assoc(). Link to comment https://forums.phpfreaks.com/topic/9904-getting-field-names-from-query/#findComment-37013 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.