image


Valora:  2/5
Inicio » PHP » PHP Tutorial » Database access




Connection to the database

Once we have created the database in our server, the following step is to connect to the database from a PHP page. To this end, PHP provides us with a series of statements to access MySQL databases.

<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
   <title>PHP Example</title>
</head>
<body>
<?php
function Conectarse()
{
   if (!($link=mysql_connect("localhost","usuario","Password")))
   {
      echo "Error conectando a la base de datos.";
      exit();
   }
   if (!mysql_select_db("base_datos",$link))
   {
      echo "Error seleccionando la base de datos.";
      exit();
   }
   return $link;
}

$link=Conectarse();
echo "Conexión con la base de datos conseguida.<br>";

mysql_close($link); //cierra la conexion
?>

</body>
</html>

View code

Ejecutar ejemplo


By executing the statement mysql_connect, we are creating a link between the database and the PHP page, this link will be used later in the queries we make to the database.

Finally, once we have stopped using the link to the database, we release it with the statement mysql_close so the connection does not remain busy.






WebEstilo.com - Introduzca su e-mail y conozca las novedades. No hacemos Spam.
Google
  Web WebEstilo.com   
 
Valid HTML 4.01!
Última modificación:25 de Diciembre de 2004. Spain - España.
© 1998-2004 por Joaquin Gracia. Todos los derechos reservados.