note 97745 added to function.mssql-query

From: Date: Thu, 06 May 2010 08:46:02 +0000
Subject: note 97745 added to function.mssql-query
Groups: php.notes 
Request: Send a blank email to php-notes+get-168994@lists.php.net to get a copy of this message
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? :( ---- Server IP: 117.55.229.1 Probable Submitter: 115.64.30.237 ---- Manual Page -- http://www.php.net/manual/en/function.mssql-query.php Edit -- https://master.php.net/note/edit/97745 Del: integrated -- https://master.php.net/note/delete/97745/integrated Del: useless -- https://master.php.net/note/delete/97745/useless Del: bad code -- https://master.php.net/note/delete/97745/bad+code Del: spam -- https://master.php.net/note/delete/97745/spam Del: non-english -- https://master.php.net/note/delete/97745/non-english Del: in docs -- https://master.php.net/note/delete/97745/in+docs Del: other reasons-- https://master.php.net/note/delete/97745 Reject -- https://master.php.net/note/reject/97745 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168994) next »