Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    php
  » Writing Functions with mysql_fetch_object()
      by Michael Milano
 Page 1 of 1 
   

(Login to remove green text ads)
Writing Functions with mysql_fetch_object
The title might be a little confusing, but I'm just going to explain the mysql_fetch_object() function. A lot of programmers are either intimidated or don't see a need to use 'objects' with PHP.

The reason I find this method very handy is so I don't have to re-write any functions in case I add/edit/remove a field in my database.

First I will show an example of how one might traditionally write a function to get user data from a table. This function will return an array.

Imagine we are using a database table with userid, firstname, and lastname.
PHP Code:
<?
function getUser($userid){
  
$array = array();

  
$result mysql_query("select * from user where userid=".$userid);
  if(
$row mysql_fetch_array($result)){
    
$array['firstname'] = $row['firstname'];
    
$array['lastname'] = $row['lastname'];
  }
  
  return 
$array;
}
?>
What if we want to add an additional field, 'username', to the user table? Not only would we have to alter the database table, but we would have to modify our getUser() function.

I'll show you two ways to solve this. The first will be a method which I've used in the past. First, you describe the table to create an array of field names. Then you use that array to create the key names of your user array. Don't follow? Here is an example:
PHP Code:
<?
function getUser($userid){
  
$result mysql_query("describe user");
  while(
$row mysql_fetch_array($result)){
    
$field[] = $row[0];
  }

  
$array = array();
  
$resultmysql_query("select * from user where userid=".$userid);

  if(
$row mysql_fetch_array($result)){
    foreach(
$fields as $field){
      
// create the key name from the field name array
      
$array[$field]=$row[$field];
    }
  }
  
  return 
$array;
}
?>
With the example above, we would never have to modify the function if we modified the user table. The downside of this is that we execute 2 queries when we should really only need 1.

The mysql_fetch_object() method solves this problem if you don't mind working with objects. This mysql function assigns the field names as they are queried as properties of the object.

In short, instead of using $array['firstname'], you will use $object->firstname.

Below is an example of mysql_fetch_object().
PHP Code:
<?
function getUser($userid){
  
$result mysql_query("select * from user where userid=".$userid);
  
// object will return false if there is no more rows
  // since there is only 1 result possible with a userid
  // i do not need this in a while loop.
  
$object mysql_fetch_object($result);

  return 
$object;
}
?>
Now let's look at an example when we would get multiple users in our result. The best way to do this is create an array of objects.
PHP Code:
<?
function getUsers(){
  
$result mysql_query("select * from user");

  while(
$object mysql_fetch_object($result)){
    
$array[]=$object;
  }

  return 
$array;
}

// here is how we would use this
$user_array getUsers();
foreach(
$user_array as $user_object){
  echo 
$user_object->firstname " " $user_object->lastname "<br>\n";
}
?>
Again, the purpose of doing it this way is so that you will never have to go back and modify this function in case you modify the database table.

I hope this article has been informative. Thanks for reading.




 
 Page 1 of 1 
   

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle