Mega Code Archive

 
Categories / Php / MySQL Database
 

ASP(VBScript), from time to time, has some neat features. One is a function

called GetString, which pertains to the Recordset object. Its purpose is to pull the entire recordset into a formatted string and close the connection to free resources. This type of functionality can be quite useful in a high load environment. So I thought it would duplicate the functionality with PHP. So here it is... <? require("conn.php"); //require connection data $delimiter_string = "</td>\n\t\t<td>"; //the \n\t\t pattern is added to be able to format the source clearly $sql = "select * from tableName"; $result = conn($sql); //custom connection function - so don't freak out $data = GetString($result, $delimiter_string); //call the function echo "<table>$data</table>"; //print out the results function GetString($handle,$delimiter) { /* This function emulates the ASP GetStrings function. It creates an array of the dataset where the the array is a string with a certain delimiter ie if you were to pass a delimiter of "|" your data would be element1 | element2 | element3 for a pipe-delimited string to pass around delimiter of "</td><td>" would get you element1 </td><td> element2 </td><td> element3 to make it really easy to create a table dump of your data (Note: I added \n\t\t in the pattern to be able to see the output neatly formatted when I view the source code for it) */ if (mysql_num_rows($handle)>0){ //initialize the array $RsString = ''; //array(); //loop thru the recordset while ($rows = mysql_fetch_array($handle,MYSQL_ASSOC)) { //add some additional tags to open and close the table rows with some formattting $RsString .= "\n\t<tr>\n\t\t<td>".implode($delimiter,$rows)."</td>\n\t</tr>\n"; } //wend return $RsString; }else{ //no records in recordset so return false return false; } //end if //close the connection mysql_close($handle); } //end function ?>