Tuesday, November 07, 2006

Simplifying MySQL Results using PHP - No More Arrays

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 imagine you had 20 pieces of data, and don’t 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 variables, so now you can call on them using the name of the mysql row, without that nasty $row[''].

No comments: