note 105418 deleted from ref.pdo-dblib by danbrown
| From: | danbrown@php.net | Date: | Wed, 17 Aug 2011 15:35:19 +0000 |
| Subject: | note 105418 deleted from ref.pdo-dblib by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-182533@lists.php.net to get a copy of this message | ||
Note Submitter: artistan at gmail dot com
----
thanks to tris+php note....
and this note on unique identifier needing to cast to varchar(36)..
http://blog.dynom.nl/archives/What-do-you-get-when-you-mix;-MSSQL,-PDO-and-uniqueidentifier_20100608_57.html
<?
class mssql {
private static $statement=null;
private static $typemap=array(
'ntext' => 'text',
'bigint' => 'real',
'decimal' => 'real',
'float' => 'real',
'numeric' => 'real',
"uniqueidentifier" => 'varchar(36)'
);
public static function all_fields($table,&$db,$query='') {
try{
if(!$query) {
$query="SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_NAME=?";
}
self::$statement=$db->prepare($query);
self::$statement->execute(array($table));
$fields=array();
$need_cast=false;
while($field=self::$statement->fetch(PDO::FETCH_ASSOC)) {
//var_dump($field);
$field_quoted=self::quote_field_name($field['COLUMN_NAME']);
if(isset(self::$typemap[$field['DATA_TYPE']])) {
$fields[]='CAST('.$field_quoted.' AS
'.self::$typemap[$field['DATA_TYPE']].') AS '.$field_quoted;
$need_cast=true;
} else $fields[]=$field_quoted;
}
} catch (PDOException $e) {
echo "Failed to get DB fields: " . $e->getMessage() . "\n";
exit;
}
return ($need_cast) ? implode(', ',$fields) : '*';
}
public static function quote_field_name($field) {
$pos=strpos($field,' ');
return ($pos===false) ? $field : '['.$field.']';
}
}