Database Code

  Home arrow Database Code arrow Mimic ASPs GetString functionality
DATABASE CODE

Mimic ASPs GetString functionality
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2005-03-17

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    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 I would duplicate the functionality with PHP. So here it is...

    By : bastien

    <?
    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 kids";

    $result = conn($sql); //custom connection function - so don't phreak 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
    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Database Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE CODE ARTICLES

    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser

    Developer Shed Affiliates

     



    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap