Posted By

wattz on 10/30/07


Tagged

mysql php active record


Versions (?)

Using PHP and MyActiveRecord ( from www.wattz.net)


 / Published in: PHP
 

URL: www.wattz.net

MyActiveRecord is a stand alone db abstraction layer to simplify things between php and mysql. you can download the latest version from www.wattz.net

  1. <?php
  2. require("myActiveRecord.php");
  3.  
  4. // this assumes that you have set your db authentication up in ActiveConnect.php
  5.  
  6. $activeRecord = new ActiveRecord('users'); //create a new object based on the table users
  7. //
  8. // Now we are going to create a new user:
  9. //
  10.  
  11. // first we set values to all the fields in the Users table:
  12. $activeRecord->username = "AppleSeed";
  13. $activeRecord->firstname = "Johnny";
  14. $activeRecord->lastname = "McIntosh";
  15. $activeRecord->password = md5("myPassWord");
  16.  
  17. // Now we save the new record
  18. $activeRecord->save();
  19.  
  20. // if the save was successful then it will not die and
  21. // the id will be set to the last entry so you can
  22. // reference it with: $activeRecord->id
  23.  
  24. // Lets say we made a mistake, we need the firstname to be
  25. // John not Johnny, in this case we can update it using:
  26.  
  27. $activeRecord->firstname = "John";
  28.  
  29. // because $activeRecord->id was set to the id of the record we created,
  30. // it will update that corresponding rows related to that id.
  31. $activeRecord->save();
  32.  
  33. // now we want to update an older record, so we will need to find the user id
  34. // with the username and password and then update based on the return id.
  35.  
  36. $userid = $activeRecord->getUseridWhere("username = 'wattz' AND password='passw0rd'");
  37.  
  38. // now we have the user's id, and we want to update the users email address and save the record:
  39. $activeRecord->id = $userid;
  40. $activeRecord->email = "[email protected]";
  41. $activeRecord->save();
  42.  
  43. //...and thats it for that part
  44.  
  45. //
  46. // Now we want to get all the users that have the db field "role" set to admin
  47. // this is easy using myActiveRecord
  48. // all we have to do is:
  49.  
  50. $adminUsers = $activeRecord->findByRole("admin");
  51. // this will return all of the users in the db table with the Role "admin";
  52. // to see the results simple do a var_dump or print_r.
  53.  
  54. // to delete a row, you simply use the delete command:
  55. $activeRecord->deleteByEmail("[email protected]");
  56.  
  57. // myActiveRecord also includes simple static functions like:
  58. $adminUsers = $activeRecord->findAll(); //returns all the records in a table
  59. $adminUsers = $activeRecord->findAllWhere("role='admin"); //if you dont prefer the dynamic methods
  60.  
  61. // and if you have some strange query you need to run
  62. // there is always the straight query command:
  63. $adminUsers = $activeRecord->query("SELECT * FROM users WHERE role='admin'");
  64.  
  65. // and finally we have fieldCount, which simply gives you a total count of fields,
  66. // with our without parameters
  67.  
  68. // total number of admin users
  69. $adminUserCount = $activeRecord->fieldCount("role='admin'");
  70.  
  71. // total number of users.
  72. $userCount = $activeRecord->fieldCount();
  73.  
  74.  
  75. // There is much more about myActiveRecord that can be accomplished, visit www.wattz.net to download
  76. // and utilize php and mysql with easy.
  77. ?>

Report this snippet  

You need to login to post a comment.