Skip to content

Instantly share code, notes, and snippets.

@mrroot5
Last active November 17, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrroot5/f328d4b1ed9c0a216e48 to your computer and use it in GitHub Desktop.
Save mrroot5/f328d4b1ed9c0a216e48 to your computer and use it in GitHub Desktop.
function existe_campo ($db_conn, $tabla, $campo) {
if ($resultado = $db_conn->query('SHOW COLUMNS FROM '.$tabla.' LIKE "'.$campo.'"')) {
if($resultado->num_rows() === 1) {
return TRUE;
} else {
return FALSE;
}
}
}
$mysqli = new mysqli('localhost', 'mi_usuario', 'mi_contraseña', 'mi_bd');
if ($mysqli->connect_error) {
die('Error de Conexión (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$nombre_tabla = 'mi_tabla';
$nombre_campo = 'mi_campo';
if (existe_campo($mysqli, $nombre_tabla, $nombre_campo) ) {
echo 'El campo '.$nombre_campo.' existe';
} else {
echo 'El campo '.$nombre_campo.'no existe';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment