Jump to content


Photo

Getting field names from query?


  • Please log in to reply
3 replies to this topic

#1 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 18 May 2006 - 05:16 AM

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

    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'];
    }

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:
        $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>";
        }
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 :(

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 18 May 2006 - 05:55 AM

$row = mysql_fetch_array($result);
foreach($row as $key => $val) {
    echo "$key = $val<br />";
}


#3 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 18 May 2006 - 06:08 AM

[!--quoteo(post=374875:date=May 18 2006, 12:55 AM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ May 18 2006, 12:55 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$row = mysql_fetch_array($result);
foreach($row as $key => $val) {
    echo "$key = $val<br />";
}
[/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!

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 18 May 2006 - 10:15 PM

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().
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users