Re: freetds and iodbc to access Microsoft SQL Server 2005

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <vsankar@...>
Cc: <misc@...>
Date: Thursday, July 16, 2009 - 3:28 am

On Tue, 14 Jul 2009 18:13:56 -0500
Vijay Sankar wrote:

> I am trying to access a database hosted on a Microsoft SQL Server 2005

[....]

Here is a trivialized interface to mssql using freetds and php.
It supports reads and writes.

Dhu

<?php
/*
Copyright (C) <2005> (Bzerkley terms)

Duncan Patton a Campbell can be contacted at campbell@neotext.ca, or campbell@indx.ca.
sqNdhu.php -- general purpose access to SQL databases .. here implemented for commandline operations
php -f sqNdhu.php
line one on stdin == MSSQL/MSHOST:1433/Uname/Upass/Dbname/
line one on stdin == MYSQL/MYHOST:3306/Uname/Upass/Dbname/
line two thru n-1 == select/insert/replace...
line n == \n

uses http://www.freetds.org/ libraries

*/

$stdinline = trim(fgets(STDIN));

list($stype,$hnp,$dbu,$dbp,$dbn) = split("/", $stdinline) ;

switch ($stype)
{

case "MSSQL":
{
$db = mssql_connect($hnp,$dbu,$dbp);
if(!$db)
{
$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "Could not connect"); fwrite($handle, "\n\n"); fclose($handle);
exit("Could not connect");
}
if(!mssql_select_db($dbn,$db))
{
$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "Could not select database"); fwrite($handle, "\n\n"); fclose($handle);
exit("Could not select database");
}
/* Performing SQL query */
while($squery= trim(fgets(STDIN)))
{
//$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "MySqery:".$squery); fwrite($handle, "\n\n"); fclose($handle);
$result = mssql_query($squery) or die("Query failed : " . $squery);
if(is_resource($result))
{
$num_results = mssql_num_rows($result);
echo "\n" . $num_results;
if($num_results == 0) echo "\n";
for ($i=0; $i <$num_results; $i++)
{
while($myrow = mssql_fetch_row($result))
{
$colcnt = count($myrow);
echo "\n" . $colcnt;
for($x=0; $x < $colcnt; $x++)
{
echo sprintf("\n%d ",strlen($myrow[$x])) . $myrow[$x];
}
}
}
mssql_free_result($result);
}
else
{
echo "\n1\n1\n1 !";
}
}
mssql_close($db);
}
break;

case "MYSQL":
{
$db = mysql_connect($hnp,$dbu,$dbp);
if(!$db)
{
$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "Could not connect"); fwrite($handle, "\n\n"); fclose($handle);
exit("Could not connect");
}
if(!mysql_select_db($dbn,$db))
{
$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "Could not select database"); fwrite($handle, "\n\n"); fclose($handle);
exit("Could not select database");
}
/* Performing SQL query */
while($squery= trim(fgets(STDIN)))
{
//$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "MySqery:".$squery); fwrite($handle, "\n\n"); fclose($handle);
$result = mysql_query($squery) or die("Query failed : " . $squery);
if(is_resource($result))
{
$num_results = mysql_num_rows($result);
echo "\n" . $num_results;
if($num_results == 0) echo "\n";
for ($i=0; $i <$num_results; $i++)
{
while($myrow = mysql_fetch_row($result))
{
$colcnt = count($myrow);
echo "\n" . $colcnt;
for($x=0; $x < $colcnt; $x++)
{
echo sprintf("\n%d ",strlen($myrow[$x])) . $myrow[$x];
//$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "."); fwrite($handle, "-"); fclose($handle);
}
}
}
mysql_free_result($result);
}
else
{
echo "\n1\n1\n1 !";
}
}
mysql_close($db);
}
break;
} //end switch

fflush(STDOUT);
exit();

?>

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
freetds and iodbc to access Microsoft SQL Server 2005, Vijay Sankar, (Tue Jul 14, 7:13 pm)
Re: freetds and iodbc to access Microsoft SQL Server 2005, Duncan Patton a Campbell, (Thu Jul 16, 3:28 am)
Re: freetds and iodbc ..., Lars Nooden, (Wed Jul 15, 4:45 am)
Re: freetds and iodbc to access Microsoft SQL Server 2005, Abel Camarillo, (Wed Jul 15, 5:02 am)