TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-19-2008, 11:45 PM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default Getting structure of resultset in memory

After a SELECT query is executed, there is a resultset located in memory.

Without using 3rd-party software, is there a way to "capture" the "table structure" of the resultset into a text file for later reference?

Thanks for any help...
Dave is offline  
Reply With Quote
Old 05-23-2008, 01:52 PM   #2 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

I do not really know of a way to get the structure of a recordset, but you can get the structure of the tables involved by using the SHOW COLUMNS syntax from MySQL:
PHP Code:
<?php
    $user 
"user";
    
$pass "pass";
    
    
$link mysql_connect("localhost"$user$pass);
    
mysql_select_db("test");
    
    
$sql 'SHOW COLUMNS FROM tester like "%"';
    
    
$result mysql_query($sql$link);
    
$fldarr = array();
    
    echo 
"<pre>";
    while(
$row mysql_fetch_assoc($result))
    {
        
print_r ($row)."\n";
        
$fldarr[] = $row['Field'];
    }
    echo 
"</pre>";
    
    
$first true;
    
$recrow '';
    
    
$fp fopen("recfile.txt""w+")    // Set the file up for writing here.
    
foreach ($fldarr as $fld)
    {
        if (!
$first)
        {
            
$recrow .= ', '.$fld;
        }
        else
        {
            
$recrow .= $fld;
            
$first false;
        }
    }
    
    
fwrite($fp$recrow);             // write your line
    
fclose($fp);
    
    echo 
$recrow;
    
    
mysql_free_result($result);
    
mysql_close($link);
?>
If you notice, this syntax supports a LIKE phrase so that you can limit the number of columns retrieved.

This script will output something like this:
Code:
Array
(
    [Field] => id
    [Type] => int(10) unsigned
    [Null] => NO
    [Key] => PRI
    [Default] => 
    [Extra] => auto_increment
)
Array
(
    [Field] => testfield1
    [Type] => varchar(45)
    [Null] => NO
    [Key] => 
    [Default] => 
    [Extra] => 
)
Array
(
    [Field] => testfield2
    [Type] => varchar(45)
    [Null] => NO
    [Key] => 
    [Default] => 
    [Extra] => 
)
Array
(
    [Field] => testfield3
    [Type] => varchar(45)
    [Null] => NO
    [Key] => 
    [Default] => 
    [Extra] => 
)

id, testfield1, testfield2, testfield3
Hope this helps!
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
The Following 2 Users Say Thank You to buggabill For This Useful Post:
Dave (05-24-2008), ReSpawN (05-25-2008)
Old 05-23-2008, 02:23 PM   #3 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

I just thought of another way, but it would require that a result came back from your query. You could dump the first row of the result into an array using mysql_fetch_assoc and get the keys from the array using array_keys like this:

php Code:
.
.
.
    $sql = 'some query';

    $result = mysql_query($sql, $link);

    $row = mysql_fetch_assoc($result);

    $recset_arr = array_keys($row);
.
.
.

This would dump the key names of $row into an array that you could parse.
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
The Following User Says Thank You to buggabill For This Useful Post:
Dave (05-24-2008)
Old 05-24-2008, 12:53 PM   #4 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Buggabill --

Thanks so much for your replies. They are most helpful for me (a learner) on a number of levels...First, there are several new bits of code that I have not encountered previously (e.g., ".=" -- not easy for me to grasp, but I think I've gotten to "second grade" on it now). Secondly, the overall concept, though simple to you, is new to me and I've gone through everything step-by-step.

So I'm still at work on this -- just wanted to say thanks for now.

Dave
Dave is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:40 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design