Version: 0.1

Type: Function

Category: Databases

License: GNU General Public License

Description: small function which takes some simple parameters and returns a query much in the form a CFQUERY would in cfml. Used to help aide CF developers move towards PHP. Currently only designed for postgres but would be nice to convert to multiplatform.



<?
/*
created by: mike wolf(init)
date: November 20001

discription: small function which takes some simple parameters and
returns a query much in the form a CFQUERY would in cfml.  Used to help
aide CF developers move towards PHP.   Currently only designed for postgres but would be nice to convert to multiplatform.

*example use:
-----------------------------------------------------------------------------------
$fields=@cfoutput("select * from products order by title","server.name","dbname","dbusernam","dbpassword");
if(array_values($fields) == 0 )
	{
	for($i=0;$i<count($fields);$i++)
		{
		echo $fields["fieldname"][$i] . "<br>";
		}
	} 
else
	{
	echo "no records";
    }
--------------------------------------------------------------------------------

*/

function cfquery($SQL,$DBSERVER,$DB,$USERNAME,$PASSWORD)
{

// make a connection and get a result

$connection = pg_connect("host=$DBSERVER dbname=$DB user=$USERNAME password=$PASSWORD") or die("couldnt make a connection to DB");

// lets get the details for looping
$sql_result=pg_exec($connection,$SQL) or die("query has errors in it");
$num = pg_numrows($sql_result);
$fieldnum = pg_numfields($sql_result);

//lets get those field names and populate the array
for($i=0;$i<$fieldnum; $i++)
{
	$fieldname[]=pg_fieldname($sql_result,$i);
	$fieldname2[pg_fieldname($sql_result,$i)][]="";
	while($x<$num)
	{
	$row=pg_fetch_array($sql_result,$x);
	 $fieldname2[pg_fieldname($sql_result,$i)][$x].=$row[pg_fieldname($sql_result,$i)];
	$x++;
	}
	$x=0;
}

//FREE UP SOME MEMORY AND RETURN RESULTS
pg_freeresult($sql_result);
return $fieldname2;
pg_close($connection);
}

?>