note 97745 deleted from function.mssql-query by danbrown
| From: | danbrown@php.net | Date: | Thu, 06 May 2010 14:33:31 +0000 |
| Subject: | note 97745 deleted from function.mssql-query by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169010@lists.php.net to get a copy of this message | ||
Note Submitter: Erik (smoke dot gfx at gmail dot com)
----
A little function I wrote just for the fun of it, it will handle simple mssql queries.
function Query($table,$where,$wherevalue,$sort,$direction)
{
//THIS ONLY WORKS IF YOU GIVE YOUR TABLE COLUMNS NAMES YOU CAN USE AS HEADINGS
//fex. if you have a field that stores the title of a book, and you call that field by the name
foo, the column header will read foo while it should read title
//create query string
$part1 = "SELECT * FROM ".$table;
//check for where clause
if($where != '')
{
$part2 = " WHERE ".$where." = ".$wherevalue;
}
//check for sort clause
if($sort != '')
{
$part3 = " ORDER BY ".$sort." ".$direction;
}
//combine vars into query
$query = $part1.$part2.$part3;
//get results from query
$result = mssql_query($query)or die("something went wrong");
//retrieve all table field names
$getfields = mssql_query($part1);
//store max fields in array for later use in calculation
$maxfields = mssql_num_fields($getfields);
$return[0][0] = $maxfields;
// Dump all field names in array
for ($i = 0; $i < $maxfields; ++$i)
{
$return[0][$i+1] = mssql_field_name($getfields, $i);
}
//get number of results from query
$count = mssql_num_rows($result);
//store query result amount in array
$return[0][$maxfields+1] = $count;
//get query results and store in array
$row = "row";
$x = 1;
while($q = mssql_fetch_array($result))
{
for ($i = 1; $i < $maxfields; ++$i)
{
$return[$x][$i] = $q[$i];
}
$x++;
}
return $return;
//the array that gets returned will have all the data you requested from the query
//$return[0][0] will tell you how many fields there are in a row (and thus how many fields per
array row there are)
//$return[0][<amount of fields in a row +1>] will tell you the number of results you got from
the query
}
edit:no code coloration? :(