Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple PHP script to show columns name of a database table.
<?php
$db = 'Database'; //Database name
$host = 'Database_host'; //Hostname or Server ip
$user = 'USER'; //Database user
$pass = 'Password'; //Database user password
$con = mysql_connect($host,$user,$pass);
if($con){
$link = mysql_select_db($db) or die("no database").mysql_error();
$count = 0;
if($link){
$sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$db' AND TABLE_NAME = 'table_name'"; // Change the table_name your own talbe name
$result = mysql_query( $sql, $con);
if(mysql_query($sql,$con)){
echo $sql."<br> <br>";
while ($row = mysql_fetch_row($result)) {
echo "COLUMN ".++$count.": {$row[0]}<br>";
$table_name = $row[0];
}
echo "<br>Total No. of COLUMNS: " . $count;
}
else{
echo "Error in query.";
}
}
else{
echo "Database not found.";
}
}
else{
echo "Connection Failed.";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment