Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 mysql_field_table in MSSQL?

Author  Topic 

micnie_2020
Posting Yak Master

232 Posts

Posted - 2011-04-10 : 23:45:42
Anyone have function for mysql_field_table work in mssql?

Thank you.

Regards,
Micheale

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-04-10 : 23:57:26
query INFORMATION_SCHEMA.COLUMNS or sys.columns


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

micnie_2020
Posting Yak Master

232 Posts

Posted - 2011-04-11 : 00:03:22
Dear KH Tan,

I have function as below, encounter problem at Red Block:-

function search() {

global $inflect;

$from = ''.$this->_table.' as '.$this->_model.' ';
$conditions = '\'1\'=\'1\' AND ';
$conditionsChild = '';
$fromChild = '';

if ($this->_hO == 1 && isset($this->hasOne)) {

foreach ($this->hasOne as $alias => $model) {
$table = strtolower($inflect->pluralize($model));
$singularAlias = strtolower($alias);
$from .= 'LEFT JOIN '.$table.' as '.$alias.' ';
$from .= 'ON '.$this->_model.'.'.$singularAlias.'_id = '.$alias.'.id ';
}
}

if ($this->id) {
$conditions .= ''.$this->_model.'.id = \''.mssql_real_escape_string($this->id).'\' AND ';
}

if ($this->_extraConditions) {
$conditions .= $this->_extraConditions;
}

$conditions = substr($conditions,0,-4);

if (isset($this->_orderBy)) {
$conditions .= ' ORDER BY '.$this->_model.'.'.$this->_orderBy.' '.$this->_order;
}

if (isset($this->_page)) {
$offset = ($this->_page-1)*$this->_limit;
$conditions .= ' LIMIT '.$this->_limit.' OFFSET '.$offset;
}

$this->_query = 'SELECT * FROM '.$from.' WHERE '.$conditions;
#echo '<!--'.$this->_query.'-->';
$this->_result = mssql_query($this->_query, $this->_dbHandle);
$result = array();
$table = array();
$field = array();
$tempResults = array();
$numOfFields = mssql_num_fields($this->_result);
for ($i = 0; $i < $numOfFields; ++$i) {
array_push($table,mysql_field_table($this->_result, $i));
array_push($field,mssql_field_name($this->_result, $i));
}
if (mssql_num_rows($this->_result) > 0 ) {
while ($row = mssql_fetch_row($this->_result)) {
for ($i = 0;$i < $numOfFields; ++$i) {
$tempResults[$table[$i]][$field[$i]] = $row[$i];
}

if ($this->_hM == 1 && isset($this->hasMany)) {
foreach ($this->hasMany as $aliasChild => $modelChild) {
$queryChild = '';
$conditionsChild = '';
$fromChild = '';

$tableChild = strtolower($inflect->pluralize($modelChild));
$pluralAliasChild = strtolower($inflect->pluralize($aliasChild));
$singularAliasChild = strtolower($aliasChild);

$fromChild .= ''.$tableChild.' as '.$aliasChild.'';

$conditionsChild .= ''.$aliasChild.'.'.strtolower($this->_model).'_id = \''.$tempResults[$this->_model]['id'].'\'';

$queryChild = 'SELECT * FROM '.$fromChild.' WHERE '.$conditionsChild;
#echo '<!--'.$queryChild.'-->';
$resultChild = mssql_query($queryChild, $this->_dbHandle);

$tableChild = array();
$fieldChild = array();
$tempResultsChild = array();
$resultsChild = array();

if (mssql_num_rows($resultChild) > 0) {
$numOfFieldsChild = mssql_num_fields($resultChild);
for ($j = 0; $j < $numOfFieldsChild; ++$j) {
array_push($tableChild,mssql_field_table($resultChild, $j));
array_push($fieldChild,mssql_field_name($resultChild, $j));
}

while ($rowChild = mssql_fetch_row($resultChild)) {
for ($j = 0;$j < $numOfFieldsChild; ++$j) {
$tempResultsChild[$tableChild[$j]][$fieldChild[$j]] = $rowChild[$j];
}
array_push($resultsChild,$tempResultsChild);
}
}

$tempResults[$aliasChild] = $resultsChild;

mssql_free_result($resultChild);
}
}


if ($this->_hMABTM == 1 && isset($this->hasManyAndBelongsToMany)) {
foreach ($this->hasManyAndBelongsToMany as $aliasChild => $tableChild) {
$queryChild = '';
$conditionsChild = '';
$fromChild = '';

$tableChild = strtolower($inflect->pluralize($tableChild));
$pluralAliasChild = strtolower($inflect->pluralize($aliasChild));
$singularAliasChild = strtolower($aliasChild);

$sortTables = array($this->_table,$pluralAliasChild);
sort($sortTables);
$joinTable = implode('_',$sortTables);

$fromChild .= ''.$tableChild.' as '.$aliasChild.',';
$fromChild .= ''.$joinTable.',';

$conditionsChild .= ''.$joinTable.'.'.$singularAliasChild.'_id = '.$aliasChild.'.id AND ';
$conditionsChild .= ''.$joinTable.'.'.strtolower($this->_model).'_id = \''.$tempResults[$this->_model]['id'].'\'';
$fromChild = substr($fromChild,0,-1);

$queryChild = 'SELECT * FROM '.$fromChild.' WHERE '.$conditionsChild;
#echo '<!--'.$queryChild.'-->';
$resultChild = mssql_query($queryChild, $this->_dbHandle);

$tableChild = array();
$fieldChild = array();
$tempResultsChild = array();
$resultsChild = array();

if (mssql_num_rows($resultChild) > 0) {
$numOfFieldsChild = mssql_num_fields($resultChild);
for ($j = 0; $j < $numOfFieldsChild; ++$j) {
array_push($tableChild,mssql_field_table($resultChild, $j));
array_push($fieldChild,mssql_field_name($resultChild, $j));
}

while ($rowChild = mssql_fetch_row($resultChild)) {
for ($j = 0;$j < $numOfFieldsChild; ++$j) {
$tempResultsChild[$tableChild[$j]][$fieldChild[$j]] = $rowChild[$j];
}
array_push($resultsChild,$tempResultsChild);
}
}

$tempResults[$aliasChild] = $resultsChild;
mssql_free_result($resultChild);
}
}

array_push($result,$tempResults);
}

if (mssql_num_rows($this->_result) == 1 && $this->id != null) {
mssql_free_result($this->_result);
$this->clear();
return($result[0]);
} else {
mssql_free_result($this->_result);
$this->clear();
return($result);
}
} else {
mssql_free_result($this->_result);
$this->clear();
return $result;
}

}
I unable to apply above mentioned method to here. Can you advise me how to alter the portion on red block?


Thank you & Appreciate ur kindly help.

Regards,
Micheale
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-04-11 : 00:10:46
Sorry, I don't know php & mysql to advice you on this.



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -