Subject: Beg. PHP 5 > Ch. 11 - fetch_field.php
Posted By: crater Post Date: 1/2/2007 11:20:38 AM
Yet something else that doesn't work in this book! Although this time it isn't the book's fault.

At the bottom of p.430 it mentions that the max_length property of the returned object from mysql_fetch_field() always returns 0. I submitted that a s bug 'cos it's still the same in MYSQL 5.1.14.

Apparently that function has been deprecated. We should now use the SQL 'SHOW COLUMNS FROM table' or query the INFORMATION_SCHEMA.COLUMNS table.

Here's my version of fetch_field.php......

<?php
include_once "./common_db.inc";

$link_id = db_connect();
$query = "SELECT column_name, column_type, is_nullable, column_key, extra ";
$query .= "FROM INFORMATION_SCHEMA.COLUMNS ";
$query .= "WHERE table_name = 'user' AND table_schema = 'sample_db'";
$result = mysql_query($query);

if (!$result) {
    error_message(sql_error());
}

while ($query_data = mysql_fetch_array($result)) {
    echo $query_data["column_name"];

    echo " - " . $query_data["column_type"];

    if ($query_data["is_nullable"] == "NO") {
        echo " not_null ";
    } else {
        echo " null ";
    }

    if ($query_data['column_key'] == "PRI") {
        echo " primary_key ";
    } else if ($query_data['column_key'] == "UNI") {
        echo " unique ";
    } else if ($query_data['column_key'] == "MUL") {
        echo " key ";
    }

    if ($query_data["extra"]) {
        echo " " . $query_data["extra"] . " ";
    }

    echo "<br>";
}
?>

Go to topic 54242

Return to index page 76
Return to index page 75
Return to index page 74
Return to index page 73
Return to index page 72
Return to index page 71
Return to index page 70
Return to index page 69
Return to index page 68
Return to index page 67