/ Published in: PHP
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
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php require("myActiveRecord.php"); // this assumes that you have set your db authentication up in ActiveConnect.php $activeRecord = new ActiveRecord('users'); //create a new object based on the table users // // Now we are going to create a new user: // // first we set values to all the fields in the Users table: $activeRecord->username = "AppleSeed"; $activeRecord->firstname = "Johnny"; $activeRecord->lastname = "McIntosh"; // Now we save the new record $activeRecord->save(); // if the save was successful then it will not die and // the id will be set to the last entry so you can // reference it with: $activeRecord->id // Lets say we made a mistake, we need the firstname to be // John not Johnny, in this case we can update it using: $activeRecord->firstname = "John"; // because $activeRecord->id was set to the id of the record we created, // it will update that corresponding rows related to that id. $activeRecord->save(); // now we want to update an older record, so we will need to find the user id // with the username and password and then update based on the return id. $userid = $activeRecord->getUseridWhere("username = 'wattz' AND password='passw0rd'"); // now we have the user's id, and we want to update the users email address and save the record: $activeRecord->id = $userid; $activeRecord->save(); //...and thats it for that part // // Now we want to get all the users that have the db field "role" set to admin // this is easy using myActiveRecord // all we have to do is: $adminUsers = $activeRecord->findByRole("admin"); // this will return all of the users in the db table with the Role "admin"; // to see the results simple do a var_dump or print_r. // to delete a row, you simply use the delete command: // myActiveRecord also includes simple static functions like: $adminUsers = $activeRecord->findAll(); //returns all the records in a table $adminUsers = $activeRecord->findAllWhere("role='admin"); //if you dont prefer the dynamic methods // and if you have some strange query you need to run // there is always the straight query command: $adminUsers = $activeRecord->query("SELECT * FROM users WHERE role='admin'"); // and finally we have fieldCount, which simply gives you a total count of fields, // with our without parameters // total number of admin users $adminUserCount = $activeRecord->fieldCount("role='admin'"); // total number of users. $userCount = $activeRecord->fieldCount(); // There is much more about myActiveRecord that can be accomplished, visit www.wattz.net to download // and utilize php and mysql with easy. ?>
URL: www.wattz.net