Is DESCRIBE table possible with PHP?

I see on page 66 of Build Your Own DataBase Driven WebSite using PHP & MySQL by Kevin Yank, it states “Just to make sure the two columns were added properly, we should ask MySQL to describe the table to us:”

mysql> DESCRIBE Jokes;
Here it shows the | Field | Type | Null | Key | Default |
with details of 5 rows in set here

Can someone please tell me if you can use the DESCRIBE Jokes; syntax with PHP, or not? Is there some documentation on the net where I can look? I am new to programming, but keen to understand as much as possible in this book, and am working through all the php programming in this book, on my computer.

Thanks in anticipation.
Nirab

DESCRIBE is a mysql keyword which “describes” the layout of the mysql table.

http://dev.mysql.com/doc/refman/5.0/en/describe.html

Write up something like this to see how it works:


$query = 'DESCRIBE jokes';
$results = mysql_query($query) or die('Query error: ' . mysql_error());

while($row = mysql_fetch_array($results)) {
	print_r($row);
	echo '<br />';
}

That worked thanks. Is it possible to code it to even look neater? Each row looks like this, this being the first row result:-
Array ( [0] => ID [Field] => ID [1] => int(11) [Type] => int(11) [2] => [Null] => [3] => PRI [Key] => PRI [4] => [Default] => [5] => auto_increment [Extra] => auto_increment )

Definitely. You’ll see in that array dump that it gives each entry a numeric key (e.g. 0, 1, 2) and a string key (‘Field’, ‘Type’, ‘Null’). The strings correspond to the metadata for each column in your table. So in your while loop, you can check for these in the array, and print it however you want.

So instead of just print_r($row) you could do something like:


echo "<table><tr><td>Column Name</td><td>Data Type</td></tr>";

while($row = mysql_fetch_array($results)){
  echo "<tr><td>" . $row['Field'] . "</td><td>" . $row['Type'] . "</td></tr>";
}

echo "</table>";

Have the result looking neat now, thanks. Do you know where I can learn PHP & MySQL in a gradual step by step fashion? The book I have “Build YOur Own Database Driven Website Using PHP & MySQL” is very good, but I notice the results don’t always come and I find that I have to add things to the code by researching the net, before I get a result. I don’t know the reason but guess sometimes the code may be superseded since the book was printed.