<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jorge Albaladejo &#187; connection</title>
	<atom:link href="http://jorgealbaladejo.com/tag/connection/feed/" rel="self" type="application/rss+xml" />
	<link>http://jorgealbaladejo.com</link>
	<description>Hard &#38; Soft design...</description>
	<lastBuildDate>Mon, 06 Feb 2012 22:00:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Connecting to a MySQL database from PHP</title>
		<link>http://jorgealbaladejo.com/2007/05/18/conexion-a-una-base-de-datos-mysql/</link>
		<comments>http://jorgealbaladejo.com/2007/05/18/conexion-a-una-base-de-datos-mysql/#comments</comments>
		<pubDate>Fri, 18 May 2007 23:23:44 +0000</pubDate>
		<dc:creator>Jorge Albaladejo</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Architecture]]></category>
		<category><![CDATA[connection]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://labs.abc-webs.net/2007/05/18/conexion-a-una-base-de-datos-mysql/</guid>
		<description><![CDATA[Notice: Article only available in Spanish! El primer paso para trabajar con scripts y aplicaciones en php que conectan con bases de datos mysql, es crear una conexión. Una conexión en php devuelve un identificador que será utilizado para cada transacción, consulta, etc, que se realice sobre la base de datos. Para comenzar, hacen falta [...]]]></description>
			<content:encoded><![CDATA[<blockquote style="text-align: justify;"><p><strong>Notice</strong>: Article only available in <strong>Spanish</strong>!</p></blockquote>
<p style="text-align: justify;">El primer paso para trabajar con scripts y aplicaciones en php que conectan con bases de datos mysql, es crear una conexión. Una conexión en php devuelve un identificador que será utilizado para cada transacción, consulta, etc, que se realice sobre la base de datos.</p>
<p style="text-align: justify;">Para comenzar, hacen falta tres datos del proveedor de servicios de alojamiento: nombre de la base de datos, nombre de usuario y contraseña de acceso. En php desde la versión 3 existe la función mysql_connect();</p>
<p style="text-align: justify;"><span id="more-16"></span></p>
<p style="text-align: justify;"><code>mysql_connect($host,$usuario,$clave);</code></p>
<p style="text-align: justify;">Donde host suele ser &#8216;localhost&#8217; (a menos que se quiera conectar a bases de datos en distintas máquinas, en cuyo caso el proveedor de servicio debe proporcionar este dato), y usuario y clave son los datos de acceso a dicho servidor de base de datos.</p>
<p style="text-align: justify;">Una vez establecida la conexión, se obtiene un identificador que se usará en cada operación que se realice sobre la base de datos. El siguiente paso es seleccionar una base de datos. Ésta debe haber sido creada de antemano, y el usuario con el que se realiza la conexión debe tener permisos de acceso a ella.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code4'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p164"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p16code4"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$identifier</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_connect" rel="external"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span><span style="color: #000088;">$usuario</span><span style="color: #339933;">,</span><span style="color: #000088;">$clave</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/mysql_select_db" rel="external"><span style="color: #990000;">mysql_select_db</span></a> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$database</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$identifier</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">Por último, una vez terminada la ejecución, hay que liberar los recursos utilizados cerrando la conexión</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code5'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p165"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p16code5"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/mysql_close" rel="external"><span style="color: #990000;">mysql_close</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$identifier</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p style="text-align: justify;">A partir de ahora, todas las consultas realizadas se efectuarán sobre la base de datos seleccionada. Un script completo para conexión a base de datos quedaría como sigue:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?" rel="external"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code6'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p166"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code" id="p16code6"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * script to connect to a database
 * @author Jorge Albaladejo Pomares [correo@jorgealbaladejo.com]
 * @license Creative Commons License http://creativecommons.org/licenses/by-sa/3.0/
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//variables</span>
<span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;john_smith&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;h01y_GRail&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;pastafarian_food&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//connection</span>
<span style="color: #000088;">$identifier</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><a href="http://www.php.net/mysql_connect" rel="external"><span style="color: #990000;">mysql_connect</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span><span style="color: #000088;">$user</span><span style="color: #339933;">,</span><span style="color: #000088;">$pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$identifier</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <a href="http://www.php.net/die" rel="external"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error connecting to database: &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/mysql_error" rel="external"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//selecting database</span>
<span style="color: #000088;">$db_selected</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_select_db" rel="external"><span style="color: #990000;">mysql_select_db</span></a> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$db</span><span style="color: #339933;">,</span> <span style="color: #000088;">$identifier</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$db_selected</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <a href="http://www.php.net/die" rel="external"><span style="color: #990000;">die</span></a> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cannot use database: &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error" rel="external"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Close connection</span>
<a href="http://www.php.net/mysql_close" rel="external"><span style="color: #990000;">mysql_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$identifier</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jorgealbaladejo.com/2007/05/18/conexion-a-una-base-de-datos-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

