Jump to content


Photo
- - - - -

[PHP] - [Simplifying MySQL Results] - [Apache]


  • Please log in to reply
7 replies to this topic

#1 Apache

Apache

    P2L Jedi

  • Twodded Staff
  • PipPipPip
  • 778 posts
  • Location:London, UK

Posted 23 March 2005 - 06:23 PM

Now we all know this sort of query to get some simple from the Database:

include('connect.php');
$query = "SELECT * FROM table WHERE name = 'Apache'";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result) {
    echo $row['name'];
}


NOw this is a great way, but imagin you had 20 pieces of data, and dont want to write $row[''] every time. Well here the solution

include('connect.php');
$query = "SELECT * FROM table WHERE name = 'Apache'";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result) {
    extract($row);
    echo $name;
}


This simple line of code pulls the $row out of the variablkes, so now you can call on them using the name of the mysql row, without that nasty $row['']

#2 _*adrian_*

_*adrian_*
  • Guests

Posted 18 May 2005 - 10:53 AM

Will this work with register_globals off?

#3 Ruben K

Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 18 May 2005 - 12:03 PM

This has no point.

#4 MalDON

MalDON

    Young Padawan

  • Members
  • Pip
  • 127 posts
  • Location:Southern California
  • Interests:DeviantART, PHP, Techno, Empire Earth 2, Halo 2

Posted 18 May 2005 - 07:33 PM

yea, it does. Although, I name all my viables first that way i know what I need rather than doing it this way.

#5 rus321

rus321

    Young Padawan

  • Members
  • Pip
  • 49 posts

Posted 10 February 2006 - 06:16 AM

this is really good... my site im working on i had to extract each 1 and i had about 20 fields...this comes in handy

#6 austen

austen

    P2L Jedi

  • Members
  • PipPipPip
  • 910 posts
  • Location:Montana, US
  • Interests:Web design, snowboarding (lots of it), Computer science related.

Posted 10 February 2006 - 11:56 AM

This would be a big help to those who are semi-new to PHP as it was for me at that time, but for experienced PHP developers, this should be common knowledge.

#7 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 13 February 2006 - 03:01 AM

I like this one, since I have about 20 nasty $row['']īs in some of my systems.

#8 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 17 May 2006 - 03:28 PM

NOw this is a great way, but imagin you had 20 pieces of data, and dont want to write $row[''] every time. Well here the solution



Or you were using mysql_fetch_object and hadto write $row -> field; :) :D

I've never used this as i deem it pointless, if you gonna code something well then time isnt an issue and just removing $row[''] had no effect on load time really. Also say you did a query within a query then used extract but they had the same field names your kinda screwed. :D Enter $row[''] :D

BTW: Why does everyone use $row[''] lol. how about $madbannanstickytoffepudding[''] lol. Kiddin.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users