Jump to content

Archived

This topic is now archived and is closed to further replies.

Prismatic

Getting field names from query?

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 :(

Share this post


Link to post
Share on other sites
[code]
$row = mysql_fetch_array($result);
foreach($row as $key => $val) {
    echo "$key = $val<br />";
}
[/code]

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites

×

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.