Last active
August 7, 2019 17:14
-
-
Save parzibyte/5554190d0ccc5033284736a2b6f6cd57 to your computer and use it in GitHub Desktop.
Obtener columnas de una tabla SQL con PHP - https://parzibyte.me/blog/2018/10/25/obtener-columnas-de-tabla-mysql-php/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function obtenerColumnasDeUnaTabla($host, $usuario, $pass, $nombreDeLaBaseDeDatos, $nombreDeLaTabla) | |
{ | |
try { | |
$base_de_datos = new PDO("mysql:host=$host;dbname=$nombreDeLaBaseDeDatos", | |
$usuario, $pass); | |
} catch (Exception $e) { | |
echo "Ocurrió algo con la base de datos: " . $e->getMessage(); | |
} | |
return $base_de_datos | |
->query("SELECT COLUMN_NAME AS columna, COLUMN_TYPE AS tipo | |
FROM information_schema.columns WHERE | |
table_schema = '$nombreDeLaBaseDeDatos' | |
AND table_name = '$nombreDeLaTabla'") | |
->fetchAll(PDO::FETCH_OBJ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment