05-23-2008, 01:52 PM
|
#2 (permalink)
|
|
The Contributor
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
|
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
|
|
|
|