Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating an array from an SQL table description

Status
Not open for further replies.

maslett

Technical User
Mar 11, 2004
121
GB
Hi,

I'm trying to create an array from an SQL table description.

When I run this:
Code:
$res = mysql_query("DESCRIBE $tablename");
while($row = mysql_fetch_array($res)) {
    echo "{$row['Field']} - {$row['Type']}<br>";
}

I get:
Code:
name - varchar(20)
age - int(3)

...I'd like to get the results from the "describe" into an array.

Any ideas?


 
Code:
	while($row = mysql_fetch_array("describe names;")) {
	  $field[]=$row;
	}
	print_r($field);

Didn't work... Output is:
NULL
 
that was not the loop I referred to. compare your first post and your second and then add the code I posted to the loop in the first post
 
$row is already an array. ('mysql_fetrch_array')

Is there something you are wanting it to do that it isn't?
 
Eh?

The loop in the first post is pretty much the same as the loop in the second post - they differ only in what happens between the brackets.

To quote yourself: "that was not the loop I referred to" - so which loop were you referring to? There's only one loop.

Sorry for being such a doofus.
 
I guess I want to "implode" the result from the sql "describe" into the array.
 
But $row is already an array. You don't need to do anything

And this is completely wrong, in fact you should be getting errors from that:
Code:
while($row = mysql_fetch_array("describe names;")) {

The mysql_fetch_array takes a result handle from a call to mysql_query, not a query directly.

So again, run your query like in your first post:
Code:
$res = mysql_query("DESCRIBE $tablename");
while($row = mysql_fetch_array($res)) {
    echo "{$row['Field']} - {$row['Type']}<br>";
}

and since $row is already an array, just stick it in a variable that you can re-use any time you want like jpadie suggested:

Code:
$res = mysql_query("DESCRIBE $tablename");
while($row = mysql_fetch_array($res)) {
    $field_array[]=$row;
}

then you can use thew $field_array array wherever you want and it will contain all the rows returned from your "describe" query.

$field_array[0]['Field']
$field_array[0]['Type']
$field_array[1]['Field']
$field_array[1]['Type']
...


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
maslett said:
The loop in the first post is pretty much the same as the loop in the second post - they differ only in what happens between the brackets.

nope. they are very different.

in the first loop you are iterating over a recordset created by mysql_query. in the second loop you have created no recordset but are using a mysql_fetch function on a string.

 
You learn something new every day! Cheers :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top