Help - Search - Members - Calendar
Full Version: [PHP] - [Simplifying MySQL Results] - [Apache]
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
Apache
Now we all know this sort of query to get some simple from the Database:

QUOTE
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

QUOTE
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['']
adrian
Will this work with register_globals off?
Ruben K
This has no point.
MalDON
yea, it does. Although, I name all my viables first that way i know what I need rather than doing it this way.
rus321
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
austen
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.
Indigo
I like this one, since I have about 20 nasty $row['']īs in some of my systems.
Matthew.
QUOTE(Apache @ Mar 24 2005, 12:22 AM) *
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; tongue.gif victory.gif

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. bigwink.gif Enter $row[''] smile.gif

BTW: Why does everyone use $row[''] lol. how about $madbannanstickytoffepudding[''] lol. Kiddin.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.