Professional Documents
Culture Documents
similitud a Microsoft ADO lo hace fácil de aprender a quienes ya han sido expuesto a la
tecnología de MS
Primitivas Básicas
• AdoNewConnection(‘Database Type’);
• Execute(‘SQL Query’);
$rs = $conn->execute($sql,array('Chocolate'));
$rs = $conn->execute($sql,array('Vanilla'));
Prepare
<?php
include("adodb.inc.php");
$db = NewADOConnection("$database_type");
$db->Connect("localhost", "john", "doe", "db278") or die("Unable to connect!");
$query = $db->Prepare("INSERT INTO library (title, author) VALUES (?, ?)");
$data = file("list.txt");
foreach ($data as $l)
{
// split on comma
$arr = explode(",", $l);
// insert values into prepared query
$result = $db->Execute($query, array($arr[0], $arr[1])) or die("Error in query:
$query. " . $db->ErrorMsg());
}
$db->Close;
?>
El RecordSet de ADOdb
• la sentencia ADOConnection–>Execute($sql) devuelve un RecordSet
$db = NewADOConnection("$database_type");
$db->Connect("$host", "$user", "$password", "employees");
$sql = "SELECT surname, age FROM employees";
$db->SetFetchMode(ADODB_FETCH_ASSOC); // Return associative array
$rs = $db->Execute($sql);
if (!$rs) { print $db->ErrorMsg(); // display error if no results could be returned
}
// loop through results
while ($row = $rs->FetchNextObject()) {
// The field names need to be uppercase
print $row->SURNAME." ".$row->AGE."<BR>";
}
Actualización de la BD
• Inserción
$employee_surname = $db->qstr("d'Angelo");
$arrival_time = $db->DBDate(time());
$sql = "INSERT INTO employee_arrival (arrival_time,surname) values
($arrival_time,$employee_surname)";
if (!($db->Execute($sql))) {
print 'Error inserting: '.$db->ErrorMsg().'<BR>';
}
Actualización
$db->Connect('localhost','root','','xphplens');
$sql = "select * from adoxyz ";
$pager = new ADODB_Pager($db,$sql);
$pager->Render($rows_per_page=5);
$rs ->GetMenu('menu1',array('A','B'),false)
Mas Info sobre ADOdb
• Home Page http://adodb.sourceforge.net/
• Manual http://phplens.com/adodb/
• FAQ http://adodb.sourceforge.net/adodb-faq.html
• Otros
• http://www.aspfree.com/c/a/Database/Accessing-Databases-with-ADODB/
• http://www.devshed.com/c/a/PHP/PHP-Application-Development-With-ADODB-part-1/
• http://www.devshed.com/c/a/PHP/PHP-Application-Development-With-ADODB-part-
2/
• http://www.devshed.com/c/a/PHP/PHP-Application-Development-With-ADODB-part-
2/
• el formato de fechas
$conn->BeginTrans();!
$ok = $conn->Execute($sql);
if ($ok) $ok = $conn->Execute($sql2);
if (!$ok) $conn->RollbackTrans();
else $conn->CommitTrans();
$conn->StartTrans();
$conn->Execute($sql);
$conn->Execute($Sql2);
$conn->CompleteTrans();