Home arrow PHP arrow Page 8 - PHP Application Development With ADODB (part 1)

Strange Relationships - PHP

PHP comes with a different API for different database types -whcih usually means a code rewrite every time your databaseadministrator decides to experiment with something new. But fear not -help is at hand, in the unlikely form of ADODB, a powerful databaseabstraction library for PHP applications.

TABLE OF CONTENTS:
  1. PHP Application Development With ADODB (part 1)
  2. A Little Insulation
  3. The Bookworm Turns
  4. Anatomy Class
  5. Different Strokes
  6. Getting It All
  7. Playing The Field
  8. Strange Relationships
  9. Hitting The Limit
  10. Coming Soon, To A Screen Near You
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 8
July 24, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
In case you're performing an INSERT query on a table containing an auto-increment primary key, you can obtain the last auto-increment ID generated via ADODB's Insert_ID() method.

<?php // uncomment this to see plaintext output in your browser // header("Content-Type: text/plain"); // include the ADODB library include("adodb.inc.php"); // create an object instance // configure library for a MySQL connection $db = NewADOConnection("mysql"); // open connection to database $db->Connect("localhost", "john", "doe", "db278") or die("Unable to connect!"); // execute query $title = $db->qstr("It's Not Me, It's You!"); $author = $db->qstr("J. Luser"); $query = "INSERT INTO library (title, author) VALUES ($title, $author)"; $result = $db->Execute($query) or die("Error in query: $query. " . $db->ErrorMsg()); // print auto-generated ID if ($result) { echo "Last inserted ID is " . $db->Insert_ID(); } // clean up $db->Close(); ?>
Note also the qstr() method, which comes in handy when you need to escape special characters in your query string.

If you're executing a query that affects the table, either by adding, deleting or modifying rows, the Affected_Rows() method can come in handy to tell you the number of rows affected.

<?php // uncomment this to see plaintext output in your browser // header("Content-Type: text/plain"); // include the ADODB library include("adodb.inc.php"); // create an object instance // configure library for a MySQL connection $db = NewADOConnection("mysql"); // open connection to database $db->Connect("localhost", "john", "doe", "db278") or die("Unable to connect!"); // execute query $query = "DELETE FROM library WHERE author = 'J. Luser'"; $result = $db->Execute($query) or die("Error in query: $query. " . $db->ErrorMsg()); // return number of affected rows if ($result) { echo $db->Affected_Rows() . " rows deleted"; } // clean up $db->Close(); ?>


 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: