<?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>blog.mycado.fr &#187; Programming</title>
	<atom:link href="http://blog.mycado.fr/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mycado.fr</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 28 Oct 2009 12:21:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>DB2 queries with PHP</title>
		<link>http://blog.mycado.fr/2009/08/utilisation-de-db2-avec-php/</link>
		<comments>http://blog.mycado.fr/2009/08/utilisation-de-db2-avec-php/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 06:25:45 +0000</pubDate>
		<dc:creator>Stéphane Journot</dc:creator>
				<category><![CDATA[IBM DB2]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[blob]]></category>
		<category><![CDATA[DB2]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.mycado.fr/?p=37</guid>
		<description><![CDATA[You want to use PHP as programmation language to query your DB2 databases. We will see how to do. We only need a HTTP server with the PECL ibm_db2 extension. This extension allow us to use new functions, relative to IBM DB2, but also work with IBM Cloudscape and Apache Derby.
After the installation, we will [...]]]></description>
			<content:encoded><![CDATA[<p>You want to use PHP as programmation language to query your DB2 databases. We will see how to do. We only need a HTTP server with the <a title="PECL IBM_DB2" href="http://pecl.php.net/package/ibm_db2" target="_blank">PECL ibm_db2</a> extension. This extension allow us to use new functions, relative to IBM DB2, but also work with IBM Cloudscape and Apache Derby.</p>
<p>After the installation, we will use these functions as normal PHP functions. This is a connection example to the SAMPLE database. You can click on the function to get more information:</p>
<pre>&lt;?php
$conn = <a title="PHP db2_connect" href="http://fr.php.net/manual/en/function.db2-connect.php" target="_blank">db2_connect</a>('SAMPLE', 'db2user', 'secretpass');

if($conn) {
  echo "connection to sample: ok.";
} else {
  echo "connection to sample: failed.";
}

<a title="PHP db2_close" href="http://fr.php.net/manual/en/function.db2-close.php" target="_blank">db2_close</a>($conn);
?&gt;</pre>
<p>Nothing very complicated here, we connect to the SAMPLE database with the <strong>db2_connect</strong> function, and we check if the connection works, then we close the connection with <strong>db2_close</strong>.</p>
<p>Now, let&#8217;s do something more interesting, do some query on our tables !</p>
<pre>&lt;?php
$query = "SELECT * FROM ADMINISTRATOR.EMPLOYEE";
$stmt = <a title="PHP db2_prepare" href="http://fr3.php.net/manual/en/function.db2-prepare.php" target="_blank">db2_prepare</a>($conn, $query);

if($stmt) {
  $ex = <a title="PHP db2_execute" href="http://fr3.php.net/manual/en/function.db2-execute.php" target="_blank">db2_execute</a>($stmt);
  if($ex) {
    while($ligne = <a title="PHP db2_fetch_array" href="http://fr3.php.net/manual/en/function.db2-fetch-array.php" target="_blank">db2_fetch_array</a>($stmt)) {
      $lastname = ligne[3];
      echo "&lt;br /&gt;- $lastname";
    }
  }
}
?&gt;</pre>
<p>We put your query in the $query variable, and we use the <strong>db2_prepare</strong> function with the previous connection ($conn). This function will &#8220;prepare&#8221; (I&#8217;m so smart), it will create an optimized path in DB2, to be more fast. We execute this result ($stmt) with the <strong>db2_execute</strong> function, who do the query on your database. To finish, we use <strong>db2_fetch_array</strong> to retrieve our data in an array.</p>
<p>A BLOB example.. or Binary Large OBject, an image, a audio or video file,..</p>
<pre>$filename = '/home/mycado/itsme.jpg';
$name = 'My cute picture";

$query = 'INSERT INTO photo (id, name, image) VALUES (?, ?, ?)';
$stmt = <a title="PHP db2_prepare" href="http://fr.php.net/manual/en/function.db2-prepare.php" target="_blank">db2_prepare</a>($conn, $query);

if($stmt) {
  <a title="PHP db2_bind_param" href="http://fr.php.net/manual/en/function.db2-bind-param.php" target="_blank">db2_bind_param</a>($stmt, 1, 'id', DB2_PARAM_IN);
  <a title="PHP db2_bind_param" href="http://fr.php.net/manual/en/function.db2-bind-param.php" target="_blank">db2_bind_param</a>($stmt, 2, 'name', DB2_PARAM_IN);
  <a title="PHP db2_bind_param" href="http://fr.php.net/manual/en/function.db2-bind-param.php" target="_blank">db2_bind_param</a>($stmt, 3, 'filename', DB2_PARAM_FILE);

  $ex = <a title="PHP db2_execute" href="http://fr.php.net/manual/en/function.db2-execute.php" target="_blank">db2_execute</a>($stmt);
}</pre>
<p>We use the flag <strong>db2_bind_param</strong> we give us more precision with the data type in our request. The first two variables, an id and a string, use DB2_PARAM_IN, an classical input parameter et we put your picture with <strong>DB2_PARAM_FILE</strong>. Note that the question marks are not an error !</p>
<p>It&#8217;s enough for now, I advice you to read the ebook &#8220;<a title="IBM Redbook PHP DB2" href="http://www.redbooks.ibm.com/abstracts/sg247301.html?Open" target="_blank">DB2 Express-C: The Developer Handbook for XML, PHP, C/C++, Java, and .NET</a>&#8216;. About the ibm_db2 functions, you can find them all on <a title="PHP ibm_db2 functions" href="http://php.net/manual/en/ref.ibm-db2.php" target="_blank">php.net</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mycado.fr/2009/08/utilisation-de-db2-avec-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
