Page 252 - Open Soource Technologies 304.indd
P. 252
Web Technologies-I
Notes 10.4.4 Details about a Query Response
Four PEAR DB methods provide you with information on a query result object: numRows( ),
numCols( ), affectedRows( ), and tableInfo( ).
The numRows( ) and numCols( ) methods tell you the number of rows and columns returned
from a SELECT query:
$howmany = $response->numRows( );
$howmany = $response->numCols( );
The affectedRows( ) method tells you the number of rows affected by an INSERT, DELETE, or
UPDATE operation:
$howmany = $response->affectedRows( );
The tableInfo( ) method returns detailed information on the type and flags of fields returned
from a SELECT operation:
$info = $response->tableInfo( );
The following code dumps the table information into an HTML table:
// connect
require_once(‘DB.php’);
$db = DB::connect(“mysql://librarian:password@localhost/library”);
if (DB::iserror($db)) {
die($db->getMessage( ));
}
$sql = “SELECT * FROM BOOKS”;
$q = $db->query($sql);
if (DB::iserror($q)) {
die($q->getMessage( ));
}
$info = $q->tableInfo( );
a_to_table($info);
function a_to_table ($a) {
echo “<html><head><title> Table Info </title></head>”;
echo “<table border=1>\n”;
foreach ($a as $key => $value) {
echo “<tr valign=top align=left><td>$key</td><td>”;
if (is_array($value)) {
a_to_table($value);
} else {
print_r($value);
}
246 LOVELY PROFESSIONAL UNIVERSITY