Jump to content

Getting field names from query?


Prismatic

Recommended Posts

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
Share on other sites

[!--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 84

84: 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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.