Buscar este blog

miércoles, 16 de octubre de 2013

CONEXION BASE DE DATOS CON PHP


<?php
$dbhost = "localhost";
$dbusuario = "root";
$dbpassword = "";
$db = "rtb_bd";
$conexion = 0;


//$GLOBALS Significa simplemente que es una variable que está disponible en cualquier parte del script. Cuando quiero utilizar una variable declarada fuera de una función
function conectarBD()
{
$GLOBALS['conexion'] = mysql_connect($GLOBALS['dbhost'], $GLOBALS['dbusuario'], $GLOBALS['dbpassword']);
mysql_select_db($GLOBALS['db'], $GLOBALS['conexion']);
}
function cerrarBD()
{
mysql_close($GLOBALS['conexion']);
}
?>

_________________________________________________________________________________

<!--actividad16.php-->
<html>
<head><title>Conexión con MySql</title></head>
<body>
<center>
<h1><font color='blue'>Información del gestor MySQL</font></h1>
<h3> Recorrido de Cursores</h3>

<!--Departamentos-->

<br /><hr /><br />
<table border='1'>
  <caption style='font-size:16pt'>Departamentos</caption>
  <tr><th>Codigo</th><th>Descripción</th><th>Situación</th></tr>
<?php
  //Establecimiento de la conexión 
  $conex = mysql_connect("localhost", "root", "nicolasa") 
           or die("NO se pudo realizar la conexión");
  
  // Selección de la base de datos
  mysql_select_db("test")
            or die("ERROR con la base de datos");
  
  //Preparación y ejecución de la consulta
  $consulta = "SELECT * FROM Departamentos";
  $resultado = mysql_query($consulta);
 
  //Recorrido del cursor de fila en fila
  while ($fila = mysql_fetch_array($resultado))
  {
     //Proceso de cada una de las filas
?>
     <tr>       
     <td> <?php echo $fila['CodDpto']; ?></td>
     <td> <?php echo $fila['Descripcion']; ?></td>
     <td> <?php echo $fila['Situacion']; ?></td>
     </tr>                                               
       
<?php 

  }
?>

 </table> <br /> <hr />

<!--Empleados-->

<br />
<table border='1'>
  <caption style='font-size:16pt'>Empleados</caption>
  <tr><th>Codigo</th><th>Nombre</th><th>Categoria</th>
      <th>Sueldo</th><th>Departamento</th></tr>
<?php
  
  //Preparación y ejecución de la consulta
  $consulta = "SELECT * FROM Empleados";
  $resultado = mysql_query($consulta);
 
  //Recorrido del cursor de fila en fila
  while ($fila = mysql_fetch_array($resultado)){
     //Proceso de cada una de las filas
     echo "<tr>";       
     echo "<td>", $fila['CodEmp'], "</td>";
     echo "<td>", $fila['Nombre'], "</td>";
     echo "<td>", $fila['Categoria'], "</td>";
     echo "<td>", $fila['Sueldo'], "</td>";
     echo "<td>", $fila['CodDpto'], "</td>";
     echo "</tr>\n";                                                 
     }    

 
  // Se cierra la conexion
  mysql_close();

?>
 </table> <br /> <hr />


</body>
</html>

No hay comentarios:

Publicar un comentario