<?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>Swekee Team Developer &#187; procedimientos</title>
	<atom:link href="http://developer.swekee.com/tag/procedimientos/feed/" rel="self" type="application/rss+xml" />
	<link>http://developer.swekee.com</link>
	<description>Blog de los desarrolladores de Swekee</description>
	<lastBuildDate>Fri, 05 Sep 2008 02:43:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Procedimientos Almacenados de MYSQL en PHP</title>
		<link>http://developer.swekee.com/2008/08/29/procedimientos-almacenados-de-mysql-en-php/</link>
		<comments>http://developer.swekee.com/2008/08/29/procedimientos-almacenados-de-mysql-en-php/#comments</comments>
		<pubDate>Sat, 30 Aug 2008 02:09:47 +0000</pubDate>
		<dc:creator>david.alvaro</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[MYSQL]]></category>
		<category><![CDATA[mysqli]]></category>
		<category><![CDATA[procedimientos]]></category>

		<guid isPermaLink="false">http://developer.swekee.com/?p=33</guid>
		<description><![CDATA[Luego de una semana agotadora de trabajo, y ya que estoy mas relajado me anime a escribir acerca de cómo utilizar procedimientos almacenados de MYSQL 5 en PHP 5, esto trae grandes beneficios en varios aspectos como la reutilización de codigo, mejor orden al momento de estudiar y entender el sistema(modularidad) y muchas cosas mas [...]]]></description>
			<content:encoded><![CDATA[<p>Luego de una semana agotadora de trabajo, y ya que estoy mas relajado me anime a escribir acerca de cómo utilizar procedimientos almacenados de <strong>MYSQL 5</strong> en <strong>PHP 5</strong>, esto trae grandes beneficios en varios aspectos como la reutilización de codigo, mejor orden al momento de estudiar y entender el sistema(modularidad) y muchas cosas mas que mejor no sigo diciendo sino te aburrire jejeje.</p>
<p>En el siguiente ejemplo te muestro como llamar a un procedimiento almacenado para traer datos y mostrarlos en pantalla:</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #666666; font-style: italic;">/******** CONSULTA CON PROCEDIMIENTO ALMACENADO **********/</span>
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> mysqli<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//mysqli exclusivo para usar procedimientos almacenados</span>
<span style="color: #000088;">$db_result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;db_productos&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">=</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;call usp_ListarProductos('<span style="color: #009933; font-weight: bold;">%s</span>',<span style="color: #009933; font-weight: bold;">%d</span>);&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Leche'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #666666; font-style: italic;">/*sprintf intenta filtrar el SQL Injection, es conveniente utilizarlo, 
%s indica que se le pasara una cadena y 
%d indica que se le pasara un numero entero*/</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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: #990000;">mysqli_errno</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mySQL error <span style="color: #009933; font-weight: bold;">%s</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//si es que hubo error se muestra mensaje</span>
<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//cerramos la conexion</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysqli_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//recorriendo registro x registro</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;producto_id&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;nombre&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;precio&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'No hay registros'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">free</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//liberando memoria</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>
<strong>Explicación:</strong></p>
<ol>
<li>Instanciamos un objeto de tipo mysqli, esto es exlusivo para poder invocar a procedimientos almacenados de mysql.</li>
<li>Después, y como te das cuenta, pasamos los parámetros de conexión con la Base de Datos.</li>
<li>Luego hacemos uso de la funcion “sprintf” que intentara filtrar Inyecciones SQL, no es obligatorio usarlo pero si te lo recomiendo.</li>
<li>Posteriormente pasamos la variable $sql al metodo “query” para que se ejecute el procedimiento almacenado y hacemos un if preguntando si es que hubo un error luego cerramos la conexion.</li>
<li>Haciendo uso de $result-&gt;num_rows&gt;0 verificamos si el procedimiento devolvió registros, si es asi, entonces recorremos uno a uno y lo vamos pintando en pantalla, caso contrario mostramos un mensaje.</li>
</ol>
<p>A continuación te muestro como llamar a un procedimiento almacenado pero para hacer una operación Insert o Update (para los 2 casos es similar).</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #666666; font-style: italic;">/******** INSERT CON PROCEDIMIENTO ALMACENADO **********/</span>
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> mysqli<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//mysqli exclusivo para usar procedimientos almacenados</span>
<span style="color: #000088;">$db_result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;db_productos&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span><span style="color: #339933;">=</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;call usp_InsertarProductos('<span style="color: #009933; font-weight: bold;">%s</span>',<span style="color: #009933; font-weight: bold;">%f</span>);&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Leche descremada'</span><span style="color: #339933;">,</span><span style="color:#800080;">15.30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #666666; font-style: italic;">/*sprintf intenta filtrar el SQL Injection, es conveniente utilizarlo, 
%s indica que se le pasara una cadena y 
%f indica que se le pasara un numero con decimales*/</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</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: #990000;">mysqli_errno</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mySQL error <span style="color: #009933; font-weight: bold;">%s</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//si es que hubo error se muestra mensaje</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">affected_rows</span><span style="color: #339933;">!=-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//si se ejecuto correctamente el procedimiento almacenado</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Se inserto el registro'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'No se pudo insertar el registro'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">free</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//liberando memoria</span>
<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//cerramos la conexion</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>La unica diferencia importante es que usamos $db-&gt;affected_rows!=-1 para verificar si se ejecuto correctamente el procedimiento.</p>
<p>Si deseas puedes descargar el ejemplo completo desde <a href="https://dshop.diino.net/getafile/B8YLWMIKA89LY7MJWD6IZSXNZADRMZX/procedimientos_almacenados_de_mysql_en_php.zip" target="_blank">aquí</a>.</p>
<p>Suerte Tigre !!</p>
]]></content:encoded>
			<wfw:commentRss>http://developer.swekee.com/2008/08/29/procedimientos-almacenados-de-mysql-en-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
