How to fetch field information for all columns using MySQLi in PHP

2 Answers

0 votes
$db_host        = 'localhost';
$db_user        = 'root';
$db_password    = '';
$db_name        = 'allonpage';

$con = new mysqli('localhost', $db_user, $db_password, $db_name);

if ($con->connect_error) 
    die('Connection Error: ' . $con->connect_error);
    
$query = "SELECT * FROM images";

if ($result = $con->query($query)) 
{
    // fetch field information for all columns 
    while ($fld = $result->fetch_field()) 
    {
        echo $fld->name . " - " . $fld->table . " - " . $fld->max_length . " - " . $fld->type . "<br />";
    }
    $result->close();
}

$con->close();


/*
run: 

image_id - images - 2 - 8
href - images - 21 - 253
src - images - 69 - 253

...


*/

 



answered Jul 7, 2016 by avibootz
0 votes
$db_host        = 'localhost';
$db_user        = 'root';
$db_password    = '';
$db_name        = 'allonpage';

$con = new mysqli('localhost', $db_user, $db_password, $db_name);

if ($con->connect_error) 
    die('Connection Error: ' . $con->connect_error);
    
$query = "SELECT * FROM images";

if ($result = $con->query($query)) 
{
    // fetch field information for all columns 
    while ($fld = $result->fetch_field()) 
    {
        echo "<pre>";
        print_r($fld);
        echo "</pre>";
    }
    $result->close();
}

$con->close();


/*
run: 

stdClass Object
(
    [name] => image_id
    [orgname] => image_id
    [table] => images
    [orgtable] => images
    [def] => 
    [db] => allonpage
    [catalog] => def
    [max_length] => 2
    [length] => 20
    [charsetnr] => 63
    [flags] => 49667
    [type] => 8
    [decimals] => 0
)

stdClass Object
(
    [name] => href
    [orgname] => href
    [table] => images
    [orgtable] => images
    [def] => 
    [db] => allonpage
    [catalog] => def
    [max_length] => 21
    [length] => 255
    [charsetnr] => 8
    [flags] => 4097
    [type] => 253
    [decimals] => 0
)

...


*/

 



answered Jul 7, 2016 by avibootz
...