PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Database » DB » Bug #230

Add tableinfo()

Details

Submitted2003-11-12 19:19 UTC
Fromtpg at umich dot edu
Assigneddanielc
StatusClosed
PackageDB
PHP Version4.3.3
OSSolaris/Linux
Roadmaps(Not assigned)

Comments

[2003-11-12 19:19 UTC] tpg at umich dot edu

Description:
------------
tableinfo() is missing for Sybase. The following code will return something very much like MySQL. I wrote this originally in DB 1.3 and have now integrated it into 1.4 and 1.5. How about putting it in the next release? Thanks

/**
* Get information describing the table
*/
function tableInfo($result, $mode = null) {
$count = 0;
$id = 0;
$res = array();

/*
* depending on $mode, metadata returns the following values:
*
* - mode is false (default):
* $result[]:
* [0]["table"] table name
* [0]["name"] field name
* [0]["type"] field type
* [0]["len"] field length
* [0]["flags"] field flags
*
* - mode is DB_TABLEINFO_ORDER
* $result[]:
* ["num_fields"] number of metadata records
* [0]["table"] table name
* [0]["name"] field name
* [0]["type"] field type
* [0]["len"] field length
* [0]["flags"] field flags
* ["order"][field name] index of field named "field name"
* The last one is used, if you have a field name, but no index.
* Test: if (isset($result['meta']['myfield'])) { ...
*
* - mode is DB_TABLEINFO_ORDERTABLE
* the same as above. but additionally
* ["ordertable"][table name][field name] index of field
* named "field name"
*
* this is, because if you have fields from different
* tables with the same field name * they override each
* other with DB_TABLEINFO_ORDER
*
* you can combine DB_TABLEINFO_ORDER and
* DB_TABLEINFO_ORDERTABLE with DB_TABLEINFO_ORDER |
* DB_TABLEINFO_ORDERTABLE * or with DB_TABLEINFO_FULL
*/

// if $result is a string, then we want information about a
// table without a resultset
if (is_string($result)) {
//id = @sybase_query("SELECT * FROM $result WHERE 0 = 1",
$id = @sybase_query("SELECT * FROM $result",
$this->connection);
if (empty($id)) {
return $this->raiseError();
}
}
// This part does not work in Mysql either.
else {
echo "I do not know what to do with a result in Sybase::tableInfo<BR>\n";
return $this->raiseError();
}

$count = @sybase_num_fields($id);
for ($i=0; $i<$count; $i++) {
$f = @sybase_fetch_field($id, $i);
$res[$i]['table'] = $result;
$res[$i]['name'] = $f->name;
$res[$i]['type'] = $f->type;
$res[$i]['len'] = $f->max_length;
$res[$i]['flags'] = '';
}
if (! empty($mode)) {
$res['num_fields']= $count;
for ($i=0; $i<$count; $i++) {
if ($mode & DB_TABLEINFO_ORDER) {
$res['order'][$res[$i]['name']] = $i;
}
if ($mode & DB_TABLEINFO_ORDERTABLE) {
$res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
}
}
}

// free the result only if we were called on a table
if (is_string($result)) {
@sybase_free_result($id);
}
return $res;
}