<?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>MacLochlainns Weblog &#187; PHP</title>
	<atom:link href="http://blog.mclaughlinsoftware.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mclaughlinsoftware.com</link>
	<description>Michael McLaughlin's Technical Blog</description>
	<lastBuildDate>Wed, 28 Jul 2010 00:02:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>PHP Function Primer</title>
		<link>http://blog.mclaughlinsoftware.com/2010/04/02/php-function-primer/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/04/02/php-function-primer/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 04:17:14 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[MAMP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3689</guid>
		<description><![CDATA[Added a new PHP Function Primer for my students. This one&#8217;s not large but fits as a page better. If you&#8217;re interested it&#8217;s here. It covers: Function Definitions Creating &#38; Using Functions Pass-by-value Functions Pass-by-reference Functions Dynamic Functions Function Default Parameters Variable-length Parameter Lists]]></description>
			<content:encoded><![CDATA[<p>Added a new PHP Function Primer for my students. This one&#8217;s not large but fits as a page better. If you&#8217;re interested it&#8217;s <a href="http://blog.mclaughlinsoftware.com/php-programming/php-function-tutorial/">here</a>.</p>
<p>It covers:</p>
<ul>
<li>Function Definitions</li>
<li>Creating &amp; Using Functions</li>
</ul>
<ul style="padding-left:30px">
<li>Pass-by-value Functions</li>
<li>Pass-by-reference Functions</li>
<li>Dynamic Functions</li>
</ul>
<ul>
<li>Function Default Parameters</li>
<li>Variable-length Parameter Lists</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/04/02/php-function-primer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Tutorial Available</title>
		<link>http://blog.mclaughlinsoftware.com/2010/03/29/php-tutorial-available/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/03/29/php-tutorial-available/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 17:11:43 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[MAMP]]></category>
		<category><![CDATA[OPAL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3683</guid>
		<description><![CDATA[I finally got around to writing that PHP Tutorial. It&#8217;s a bit large and takes about 10 seconds to load or longer depending on your connection and machine. It covers the basics from writing your first page to loops. It was too large for a blog post, so it&#8217;s a blog page. Click on the [...]]]></description>
			<content:encoded><![CDATA[<p>I finally got around to writing that PHP Tutorial. It&#8217;s a bit large and takes about 10 seconds to load or longer depending on your connection and machine. It covers the basics from writing your first page to loops. It was too large for a blog post, so it&#8217;s a blog page. Click on the <a href="http://blog.mclaughlinsoftware.com/php-programming/basic-php-tutorial/">link if you&#8217;d like to check it out</a>.</p>
<p>I plan others on functions, objects, and files. Then, I&#8217;ll get to tutorials against databases.</p>
<p>Naturally, suggestions are always welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/03/29/php-tutorial-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Binding a Wildcard</title>
		<link>http://blog.mclaughlinsoftware.com/2010/02/21/php-binding-a-wildcard/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/02/21/php-binding-a-wildcard/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 07:13:31 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[MAMP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3515</guid>
		<description><![CDATA[Somebody asked me about how you bind a parameter in mysqli when a multiple character wildcard needs to be next to the variable value. They&#8217;d found that it worked when creating a SQL statement by concatenation (string gluing), like this: $sql = &#34;SELECT item_title FROM item WHERE item_title LIKE '%&#34;.$title.&#34;%'&#34;; However, they tried to bind [...]]]></description>
			<content:encoded><![CDATA[<p>Somebody asked me about how you bind a parameter in <code>mysqli</code> when a multiple character wildcard needs to be next to the variable value. They&#8217;d found that it worked when creating a SQL statement by concatenation (string gluing), like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT item_title FROM item WHERE item_title LIKE '%&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;%'&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>However, they tried to bind the variable following the same pattern, and found that it failed. They used the following type of syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT item_title FROM item WHERE item_title LIKE '%?%'&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It raised the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Warning<span style="color: #339933;">:</span> <span style="color: #990000;">mysqli_stmt_bind_param</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#91;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #339933;">.</span>mysqli<span style="color: #339933;">-</span>stmt<span style="color: #339933;">-</span>bind<span style="color: #339933;">-</span>param<span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span> Number of variables doesn<span style="color: #0000ff;">'t match number of parameters in prepared statement in program_name on line #</span></pre></div></div>

<p>The reason is the parser, it expects variables to be independent tokens in the SQL string. You can&#8217;t bind a string into the shell of an apostrophe delimited string. You could naturally, make an assignment to the local PHP variable before binding it to the variable, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'%'</span><span style="color: #339933;">.</span><span style="color: #000088;">$title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'%'</span><span style="color: #339933;">;</span></pre></div></div>

<p>It&#8217;s actually a better idea to concatenate the multiple character wildcard operator inside the SQL statement. The correct syntax requires that you use the <code>CONCAT()</code> function. You could reset to use piped concatenation but generally you should avoid that on the MySQL platform (see <a href="http://blog.mclaughlinsoftware.com/2009/03/09/sql-concatenation-blues/">this post</a> for an explanation of SQL concatenation on Oracle, MySQL, and SQL Server).</p>
<p>This is the required statement when using a MySQL database:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT item_title FROM item WHERE item_title LIKE CONCAT('%',?,'%')&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<div class="dropdownbox" style="padding-left:20px;background:#FFFFFF">
<p title="Click to see content ..." class="dropdownclick"><strong><em><span style="font-size:125%">Complete Code Sample</span></em></strong> <span>&darr;</span></p>
<p style="clear: both">
Expand this section to see the complete working code sample.</p>
<div>
<p>The first component for this program is an include file for the database credentials:</p>

<div class="wp_syntax"><div 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;">// Connection variables.</span>
  <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HOSTNAME'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'USERNAME'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;student&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PASSWORD'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;student&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DATABASE'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;sampledb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Once you&#8217;ve placed the credentials in your directory, you can put this in the same directory and then call it from your browser. At least, you can provided you&#8217;ve created the user with the required password, and the database on the standard <code>3306</code> port.</p>

<div class="wp_syntax"><div 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;">// Set database credentials.</span>
  <span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MySQLCredentials.inc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Return successful attempt to connect to the database.</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">mysqli_connect</span><span style="color: #009900;">&#40;</span>HOSTNAME<span style="color: #339933;">,</span>USERNAME<span style="color: #339933;">,</span>PASSWORD<span style="color: #339933;">,</span>DATABASE<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;">// Print user message.</span>
    <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Sorry! The connection to the database failed. Please try again later.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Assign the OCI error and format double and single quotes.</span>
    <span style="color: #b1b100;">print</span> <span style="color: #990000;">mysqli_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Kill the resource.</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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: #666666; font-style: italic;">// Declare input variables.</span>
    <span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;RoboCop&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    query_insert<span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</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;">// Query results afret an insert.</span>
  <span style="color: #000000; font-weight: bold;">function</span> query_insert<span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Return successful attempt to connect to the database.</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$c</span>  <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">mysqli_connect</span><span style="color: #009900;">&#40;</span>HOSTNAME<span style="color: #339933;">,</span>USERNAME<span style="color: #339933;">,</span>PASSWORD<span style="color: #339933;">,</span>DATABASE<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;">// Print user message.</span>
      <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Sorry! The connection to the database failed. Please try again later.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Assign the OCI error and format double and single quotes.</span>
      <span style="color: #b1b100;">print</span> <span style="color: #990000;">mysqli_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Kill the resource.</span>
      <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Initialize a statement in the scope of the connection.</span>
      <span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysqli_stmt_init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Declare a SQL SELECT statement returning a CLOB.</span>
      <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT item_title FROM item WHERE item_title LIKE CONCAT('%',?,'%')&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Prepare statement.</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysqli_stmt_prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">mysqli_stmt_bind_param</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;s&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Execute it and print success or failure message.</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysqli_stmt_execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
          <span style="color: #666666; font-style: italic;">// Store result.</span>
          <span style="color: #990000;">mysqli_stmt_store_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #666666; font-style: italic;">// Bind result to local variable.</span>
          <span style="color: #990000;">mysqli_stmt_bind_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span><span style="color: #000088;">$item_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #666666; font-style: italic;">// Open HTML table and print header.</span>
          <span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;table border=&quot;1&quot; cellpadding=&quot;3&quot; cellspacing=&quot;0&quot;&gt;'</span><span style="color: #339933;">;</span>
          <span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;tr&gt;'</span><span style="color: #339933;">;</span>
          <span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;th align=&quot;center&quot; style=&quot;font-weight:bold;padding:2px;margin:1px;background:#8DB3E2;width:300px&quot;&gt;Item Title&lt;/th&gt;'</span><span style="color: #339933;">;</span>
          <span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;/tr&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #666666; font-style: italic;">// Read result.</span>
          <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysqli_stmt_fetch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;tr&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;td style=&quot;padding:2px;margin:1px;background:#DBE5F1;&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$item_title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;/tr&gt;'</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
&nbsp;
          <span style="color: #666666; font-style: italic;">// Close the HTML table.</span>
          <span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;/table&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
          <span style="color: #666666; font-style: italic;">// Print the HTML table.</span>
          <span style="color: #b1b100;">print</span> <span style="color: #000088;">$out</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Free system resources.</span>
      <span style="color: #990000;">mysqli_stmt_free_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Disconnect from database.</span>
      <span style="color: #990000;">mysqli_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/02/21/php-binding-a-wildcard/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The ereg() function is gone</title>
		<link>http://blog.mclaughlinsoftware.com/2009/12/29/the-ereg-function-is-gone/</link>
		<comments>http://blog.mclaughlinsoftware.com/2009/12/29/the-ereg-function-is-gone/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 08:03:30 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3381</guid>
		<description><![CDATA[Alas, poor ereg() I abused you well. PHP 5.3 has deprecated ereg() and now we must move forward with preg_match(). Along with that change, somebody asked me to show how to upload images to the file system as opposed to the database. Personally, I think they should be stored in the database. With my bias [...]]]></description>
			<content:encoded><![CDATA[<p>Alas, poor <code>ereg()</code> I abused you well. PHP 5.3 has deprecated <code>ereg()</code> and now we must move forward with <code>preg_match()</code>. Along with that change, somebody asked me to show how to upload images to the file system as opposed to the database. Personally, I think they should be stored in the database.</p>
<p>With my bias toward databases, I threw in a virtual directory mapping in a MySQL database because it doesn&#8217;t natively support an Oracle equivalent <code>BFILE</code> data type. You can see <a href="http://blog.mclaughlinsoftware.com/plsql-programming/how-to-return-a-fully-qualified-bfile-file-name-from-a-function/">this older post</a> how to use the <code>DBA_DIRECTORIES</code> view in Oracle to mimic this behavior.</p>
<p>Naturally, MySQL is the preferred database of the person asking the question. You could also implement this exactly the same in Oracle but you really don&#8217;t want to do so. Using Oracle&#8217;s <em>virtual directories</em> has it&#8217;s own pre-built set of security features. They provide a more robust solution.  </p>
<p>The code is presented as follows (<a href="http://blog.mclaughlinsoftware.com/2009/05/24/mysql-installation-and-more/">setup for MySQL instructions</a>):</p>
<ol start="1">
<li>Create and seed the <code>DIRECTORY</code> table in MySQL:</li>
</ol>
<div style="margin-left:30px">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Create a directory table.</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> directory
<span style="color: #66cc66;">&#40;</span> directory_id   int <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span>
<span style="color: #66cc66;">,</span> virtual_name   varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">,</span> directory_name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Seed the table with a virtual directory mapping.</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> directory <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'CMS_IMAGES'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'C:<span style="color: #000099; font-weight: bold;">\\</span>Data'</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

</div>
<ol start="2">
<li>Create a <code>MySQLCredentials.inc</code> <em>credentails</em> file for inclusion in the PHP program:</li>
</ol>
<div style="margin-left:30px">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</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;">// Connection variables.</span>
  <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HOSTNAME'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'USERNAME'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;student&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PASSWORD'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;student&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DATABASE'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;sampledb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

</div>
<ol start="3">
<li>Create the PHP uploading program, named <code>MySQLFileUpload.php</code>:</li>
</ol>
<div style="margin-left:30px">

<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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
</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;">// Set database credentials.</span>
  <span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MySQLCredentials.inc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Declare input variables.</span>
  <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1021</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Upload a file to server in a mapped physical drive location.</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>process_uploaded_file<span style="color: #009900;">&#40;</span>map_virtual_directory<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Successfully Uploaded the file.&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Map a virtual directory to a physical directory.</span>
  <span style="color: #000000; font-weight: bold;">function</span> map_virtual_directory<span style="color: #009900;">&#40;</span><span style="color: #000088;">$virtual</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Return successful attempt to connect to the database.</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">mysqli_connect</span><span style="color: #009900;">&#40;</span>HOSTNAME<span style="color: #339933;">,</span>USERNAME<span style="color: #339933;">,</span>PASSWORD<span style="color: #339933;">,</span>DATABASE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Print user message.</span>
      <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Sorry! The connection to the database failed. Please try again later.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Return error message.</span>
      <span style="color: #b1b100;">print</span> <span style="color: #990000;">mysqli_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Kill the resource.</span>
      <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Initialize a statement in the scope of the connection.</span>
      <span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysqli_stmt_init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Declare a case insensitive dynamic SQL statement.</span>
      <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT directory_name FROM directory WHERE virtual_name = UCASE(?)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Prepare the statement.</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysqli_stmt_prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Bind the input parameter to the prepared statement.</span>
        <span style="color: #990000;">mysqli_stmt_bind_param</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'s'</span><span style="color: #339933;">,</span><span style="color: #000088;">$virtual</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Execute the prepared statement.</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysqli_stmt_execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
          <span style="color: #666666; font-style: italic;">// Bind the result to a local variable.</span>
          <span style="color: #990000;">mysqli_stmt_bind_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span><span style="color: #000088;">$directory</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #666666; font-style: italic;">// FetchPrepare statement and link it to a connection.</span>
          <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysqli_stmt_fetch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$directory</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">else</span>
          <span style="color: #666666; font-style: italic;">// Return error message.</span>
          <span style="color: #b1b100;">print</span> <span style="color: #990000;">mysqli_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span>
        <span style="color: #666666; font-style: italic;">// Return error message.</span>
        <span style="color: #b1b100;">print</span> <span style="color: #990000;">mysqli_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #666666; font-style: italic;">// Disconnect from database.</span>
      <span style="color: #990000;">mysqli_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Manage file upload.</span>
  <span style="color: #000000; font-weight: bold;">function</span> process_uploaded_file<span style="color: #009900;">&#40;</span><span style="color: #000088;">$directory</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* Assume the application may allow a virtual directory with a trailing backslash or forward
       slash to be stored in the database, and manage both scenarios across Windows and Linux. */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.Win32.&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;SERVER_SOFTWARE&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/\b<span style="color: #000099; font-weight: bold;">\\</span>\/&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$directory</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/\b\//&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$directory</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$directory</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$directory</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$directory</span><span style="color: #009900;">&#41;</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: #000088;">$directory</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$directory</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span> <span style="color: #000088;">$directory</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$directory</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">else</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/\b\//&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$directory</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$directory</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$directory</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$directory</span><span style="color: #009900;">&#41;</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: #b1b100;">else</span>
        <span style="color: #000088;">$directory</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$directory</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">;</span>  
&nbsp;
    <span style="color: #666666; font-style: italic;">// Check for, move uploaded file, and confirm processing.</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userfile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Move temporary cache into a file directory with the uploaded file name.</span>
      <span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userfile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$directory</span><span style="color: #339933;">.</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userfile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Remove this from real code, it's here for example only. ;-)</span>
      <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Uploaded [&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'userfile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;] to&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$directory</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// Return true to encapsulate the functional logic on success.</span>
	    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
      <span style="color: #666666; font-style: italic;">// Return false to encapsulate the functional logic on failure.</span>
	    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

</div>
<ol start="4">
<li>Create a web page to test it:</li>
</ol>
<div style="margin-left:30px">

<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
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;
  UploadFileFormMySQL.htm
&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id=&quot;uploadForm&quot;
      action=&quot;MySQLFileUpload.php&quot;
      enctype=&quot;multipart/form-data&quot;
      method=&quot;post&quot;&gt;
  &lt;table border=0 cellpadding=0 cellspacing=0&gt;
    &lt;tr&gt;
      &lt;td width=125&gt;Item Number&lt;/td&gt;
      &lt;td&gt;
        &lt;input id=&quot;id&quot; name=&quot;id&quot; type=&quot;text&quot;&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td width=125&gt;Select File&lt;/td&gt;
      &lt;td&gt;
        &lt;input id=&quot;uploadfilename&quot; name=&quot;userfile&quot; type=&quot;file&quot;&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td width=125&gt;Click Button to&lt;/td&gt;
      &lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;Upload File&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

</div>
<p>Hope this helps a few folks. I imagine that the prepared statement with bound variables may help a few folks because it&#8217;s not found (at writing) on the <code>php.net</code> web site.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2009/12/29/the-ereg-function-is-gone/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lobs with mysqli in PHP 5.3</title>
		<link>http://blog.mclaughlinsoftware.com/2009/12/04/lobs-with-mysqli-in-php-5-3/</link>
		<comments>http://blog.mclaughlinsoftware.com/2009/12/04/lobs-with-mysqli-in-php-5-3/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 05:59:43 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3272</guid>
		<description><![CDATA[We got away with a bit of lazy PHP, at least through PHP 5.29. Especially when we worked with LOB columns. All that is at an end with PHP 5.3. You must now call the mysqli_stmt_store_results($stmt); function (line number 76 in the program snippet below from source) between the execute and bind result phrases. The [...]]]></description>
			<content:encoded><![CDATA[<p>We got away with a bit of lazy PHP, at least through PHP 5.29. Especially when we worked with LOB columns. All that is at an end with PHP 5.3. You must now call the <code>mysqli_stmt_store_results($stmt);</code> function (line number 76 in the program snippet below from <a href="http://michaelmclaughlin.info/db1/lesson-9-lamp-deployment/mysql-and-php/">source</a>) between the execute and bind result phrases.</p>
<p>The function transfers a result set from a prepared statement. You need to use it when performing a <code>SELECT</code>, <code>SHOW</code>, <code>DESCRIBE</code>, or <code>EXPLAIN</code> statement that you will call by the <code>mysqli_stmt_fetch($stmt);</code> function.</p>
<p>Without using it, you may only see part of a <a href="http://dev.mysql.com/doc/refman/5.1/en/blob.html"><code>MEDIUMTEXT</code></a> column displayed. Likewise, an image from a <code>MEDIUMBLOB</code> column won&#8217;t be displayed at all unless you store their results too, by using the same function call.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Prepare statement.</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysqli_stmt_prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">mysqli_stmt_bind_param</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;i&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Execute it and print success or failure message.</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysqli_stmt_execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Store result, which eliminates memory cutting off lob streams.</span>
    <span style="color: #990000;">mysqli_stmt_store_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Bind result to local variable.</span>
    <span style="color: #990000;">mysqli_stmt_bind_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span> <span style="color: #000088;">$desc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Read result.</span>
    <span style="color: #990000;">mysqli_stmt_fetch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Print result.</span>
    <span style="color: #b1b100;">print</span> <span style="color: #000088;">$desc</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Also, that oldie <a href="http://php.net/manual/en/function.ereg.php"><code>ereg()</code> function</a> is deprecated for <code>preg_match()</code> in PHP 5.3 and removed in PHP 6. Don&#8217;t forget to make those changes if you were lazy like me before updating to PHP 5.3.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2009/12/04/lobs-with-mysqli-in-php-5-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows 7 and Zend CE</title>
		<link>http://blog.mclaughlinsoftware.com/2009/11/28/windows-7-and-zend-ce/</link>
		<comments>http://blog.mclaughlinsoftware.com/2009/11/28/windows-7-and-zend-ce/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 04:56:46 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Microsoft Windows 7]]></category>
		<category><![CDATA[OPAL]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3183</guid>
		<description><![CDATA[Installed Zend Community Edition on Windows 7 64-bit. It worked easily. You just need to remember to install the JSDK 32-bit version for the Java Bridge. Clear notation about phpMyAdmin and MySQL being separate downloads has been added to the new Zend Community Edition Server (4.0.6), and it clearly does support Windows 7. If you [...]]]></description>
			<content:encoded><![CDATA[<p>Installed Zend Community Edition on Windows 7 64-bit. It worked easily. You just need to remember to install the <a href="http://blog.mclaughlinsoftware.com/2009/07/07/zend-java-bridge-32-bit/">JSDK 32-bit version for the Java Bridge</a>. Clear notation about <a href="http://www.phpmyadmin.net/home_page/index.php">phpMyAdmin</a> and MySQL being separate downloads has been added to the new Zend Community Edition Server (4.0.6), and it clearly does support Windows 7.</p>
<p>If you plan on installing MySQL and Oracle, I would recommend you install MySQL after you install Oracle and the Zend Community Server. However, it doesn&#8217;t matter because both ways work. </p>
<p>That completes my WAMP (Windows, Apache, MySQL, Perl, PHP, or Python) and OPAW (Oracle, Perl, PHP, or Python, Apache, Windows) installations. Actually, I&#8217;m not sure there is an <em>OPAW</em> acronym for a <a href="http://en.wikipedia.org/wiki/LAMP_%28software_bundle%29">LAMP</a> stack running Oracle on a Windows platform. OPAL is the acronym for a LAMP stack running an Oracle database, but I&#8217;ve never seen one before for Windows. Therefore, I created one.</p>
<p><span style="font-size:125%;color:blue"><em>My two cents worth &#8230;</em></span> </p>
<p>I&#8217;d vote for clearer guidance on these acronyms. After all, they&#8217;re only purpose appears to be how to market variants of LAMP. The variants that I&#8217;ve seen for LAMP (Linux) are: MAMP (Mac OS X), SCAMP (Santa Cruz Operation), SAMP (Solaris), OAMP (OpenBSD, and WAMP (Windows) for MySQL database versions. The key seems to be swapping the first letter. I&#8217;ve only seen OPAL (Linux) officially for a LAMP stack that uses an Oracle database on a Linux platform. While my OPAW leverages what I perceive as a possible pattern, it may be wrong. Does anybody know what the right way to label these is?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2009/11/28/windows-7-and-zend-ce/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>reCAPTCHA on WordPress</title>
		<link>http://blog.mclaughlinsoftware.com/2009/07/18/recaptcha-on-wordpress/</link>
		<comments>http://blog.mclaughlinsoftware.com/2009/07/18/recaptcha-on-wordpress/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 04:08:20 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=2944</guid>
		<description><![CDATA[I put up a contact page (@Contact Me) for two reasons. Too many folks wanted to ask something and could only do so through a comment. I would review the comment, email them, et cetera. Reason two is simpler, I should have done it from the outset. There were a few gotchas beyond installing and [...]]]></description>
			<content:encoded><![CDATA[<p>I put up a contact page (<a href="http://blog.mclaughlinsoftware.com/contact-me/">@Contact Me</a>) for two reasons. Too many folks wanted to ask something and could only do so through a comment. I would review the comment, email them, et cetera. Reason two is simpler, I should have done it from the outset.</p>
<p>There were a few gotchas beyond installing and securing your public and private keys. I figured that it might be helpful to note them here, especially if I have to troubleshoot it later. <img src='http://blog.mclaughlinsoftware.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<ol start="1">
<li>You need create a template, like <code>contact.php</code> script, which should include your contact form. You&#8217;ll also embed some <a href="http://recaptcha.net/plugins/php/">reCAPTCHA</a> PHP inside this file.</li>
</ol>
<div style="padding-left:30px">
<p>Make sure that your <em>action</em> attribute points to the following location, which is discussed later in step 5.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #339933;">&lt;</span>form action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/wp-content/themes/&lt;theme name&gt;/contact_code.php&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contact&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The reCAPTCHA PHP should be the last element in your <em>form</em> tag set.</p>

<div class="wp_syntax"><div 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;">// Include the reCAPTCHA library, this assumes a relative directory.</span>
  <span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'recaptchalib.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Get a key from http://recaptcha.net/api/getkey</span>
  <span style="color: #000088;">$publickey</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;your public key value goes here&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// The response from reCAPTCHA</span>
  <span style="color: #000088;">$resp</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// The error code from reCAPTCHA, if any</span>
  <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">echo</span> recaptcha_get_html<span style="color: #009900;">&#40;</span><span style="color: #000088;">$publickey</span><span style="color: #339933;">,</span> <span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</div>
<ol start="2">
<li>You need to put the following <a href="http://codex.wordpress.org/Stepping_Into_Templates">WordPress template</a> stub at the beginning of your <code>contact.php</code> page.</li>
</ol>
<div style="padding-left:30px">

<div class="wp_syntax"><div 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;">/*
    Template Name: contact
  */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</div>
<ol start="3">
<li>After you create and position the <code>contact.php</code> file in the <code>/wp-content/themes/&lt;theme_name&gt;/</code> within the document root, you should create a new blog page. You don&#8217;t put any text in the page, rather you simply choose the <code>contact.php</code> from your list of templates. You also need a title for the page, I used <em>@contact-me</em> (the @ won&#8217;t be used in any reference in the URL), which is an alias to the page</li>
</ol>
<ol start="4">
<li>You need to put the balance of your PHP code in a separate file, like <code>contact_code.php</code> and put the URL re-write and reCAPTCHA calls here.</li>
</ol>
<div style="padding-left:30px">

<div class="wp_syntax"><div 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;">// Include the library for reCAPTCHA at the theme level in the file hierarchy.</span>
  <span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'recaptchalib.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Put any form PHP here.</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Declare your private key.</span>
  <span style="color: #000088;">$privatekey</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;put your private key here&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Check whether there a reCAPTCHA response?</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;recaptcha_response_field&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$resp</span> <span style="color: #339933;">=</span> recaptcha_check_answer<span style="color: #009900;">&#40;</span><span style="color: #000088;">$privatekey</span>
                                  <span style="color: #339933;">,</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;REMOTE_ADDR&quot;</span><span style="color: #009900;">&#93;</span>
                                  <span style="color: #339933;">,</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;recaptcha_challenge_field&quot;</span><span style="color: #009900;">&#93;</span>
                                  <span style="color: #339933;">,</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;recaptcha_response_field&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Check for valid response.</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$resp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_valid</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$to</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;email here&gt;@&lt;domain here&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$subject</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$subject</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;From: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Message: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$from</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'From:'</span><span style="color: #339933;">.</span><span style="color: #000088;">$email</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$to</span><span style="color: #339933;">,</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span><span style="color: #000088;">$message</span><span style="color: #339933;">,</span><span style="color: #000088;">$from</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Set the header to the document root when successful.</span>
        <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: /&quot;</span><span style="color: #009900;">&#41;</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: #666666; font-style: italic;">// Set the header to the virtual path for the contact page.</span>
      <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: /contact-me/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$resp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</div>
<p>That&#8217;s about it. If I forgot something, you&#8217;ll let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2009/07/18/recaptcha-on-wordpress/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>PHP for loading a BLOB</title>
		<link>http://blog.mclaughlinsoftware.com/2009/07/09/php-for-loading-a-blob/</link>
		<comments>http://blog.mclaughlinsoftware.com/2009/07/09/php-for-loading-a-blob/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 08:57:56 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[MAMP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=2824</guid>
		<description><![CDATA[Sometimes you chalk something up as straightforward because you&#8217;ve done it a while. I did that in a lab assignment recently. It asked my students to upload a large text file and image to the MySQL database, store them in a TEXT and MEDIUMBLOB column, and read them back out of the database, like this: [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you chalk something up as straightforward because you&#8217;ve done it a while. I did that in a lab assignment recently. It asked my students to upload a large text file and image to the MySQL database, store them in a <code>TEXT</code> and <code>MEDIUMBLOB</code> column, and read them back out of the database, like this:</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2009/07/mysql_lob4.png"><img src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2009/07/mysql_lob4.png" alt="mysql_lob4" title="mysql_lob4" style="border:none" width="555" height="516" class="aligncenter size-full wp-image-2821" /></a></p>
<p>The trick was that I wanted them to read the file into a string and then load the string. There wasn&#8217;t a single code example to do this out there, except some that might exist behind an account and credit card payment. I put together a complete example like the <a href="http://blog.mclaughlinsoftware.com/php-programming/oracle-lob-processing/">Oracle LOB processing page</a>. You can find it here in the <a href="http://blog.mclaughlinsoftware.com/php-programming/mysql-textblob-processing/">MySQL LOB processing blog page</a>. More or less, it shows you how to stream an image into a MySQL database in chunks (I chose 8 K chunks).</p>
<p>The general tricks to upload a string require you enclose to enclose them with the <code>addslashes()</code> function before assigning a binary stream to a variable, then <code>stripslashes()</code> function by segment before you load it to the database. You really don&#8217;t need to do that. <em>It&#8217;s a myth</em>. The binary stream doesn&#8217;t require that extra handling. In fact, you can corrupt certain images when you use the <code>addslashes()</code> and <code>stripslashes()</code> functions; they should be avoided in this context.</p>
<p>You should do it in streams (at least when they&#8217;re larger than 1 MB), I chose the procedural <code>mysqli</code> to demonstrate it because there wasn&#8217;t an example that I or my students could find on the <em>web</em>. Just for information, some laptops don&#8217;t have the resources to accommodate <code>LARGEBLOB</code> datatypes on the Windows OS. The <code>BLOB</code> or <code>MEDIUMBLOB</code> should work fine, especially for this little example.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>46
47
48
49
50
51
52
53
54
55
56
57
58
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// Declare a PL/SQL execution command.</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE item SET item_blob = ? WHERE item_id =  ?&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Prepare statement and link it to a connection.</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysqli_stmt_prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">mysqli_stmt_bind_param</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;bi&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$item_blob</span><span style="color: #339933;">,</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$chunk</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">8192</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item_blob</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">mysqli_send_long_data</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item_blob</span><span style="color: #339933;">,</span><span style="color: #000088;">$start</span><span style="color: #339933;">,</span><span style="color: #000088;">$chunk</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$start</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$chunk</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You can find the code in that blog page referenced. Hope it helps some folks.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2009/07/09/php-for-loading-a-blob/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zend Java Bridge 32-bit</title>
		<link>http://blog.mclaughlinsoftware.com/2009/07/07/zend-java-bridge-32-bit/</link>
		<comments>http://blog.mclaughlinsoftware.com/2009/07/07/zend-java-bridge-32-bit/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 03:39:14 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[MAMP]]></category>
		<category><![CDATA[Microsoft XP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[OPAL]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=2808</guid>
		<description><![CDATA[I just wanted to see how Zend Server Community Edition might be broken. Performing a full feature install on Windows x64, I confirmed that Zend Server&#8217;s Java Bridge depends on the 32-bit JRE (common sense prevails). Installing it against the JRE 64-bit jvm.dll did raised an exception but none of the instructions address the problem. [...]]]></description>
			<content:encoded><![CDATA[<p>I just wanted to see how Zend Server Community Edition might be broken. Performing a full feature install on Windows x64, I confirmed that Zend Server&#8217;s Java Bridge depends on the 32-bit JRE (common sense prevails). Installing it against the JRE 64-bit jvm.dll did raised an exception but none of the instructions address the problem.</p>
<p>It&#8217;s one of those simplifying assumptions &#8211; everybody knows 32-bit software works with 32-bit software. Anybody running on Windows XP x64 should know that they may need a JDK 64-bit and both a JRE 64-bit and JRE 32-bit for some applications. For those who don&#8217;t know this, like my students and other newbies, when you run Windows XP the 64-bit stuff goes in the <code>C:\Program Files</code> directory and the 32-bit stuff goes in the <code>C:\Program Files (x86)</code> directory. This lets you develop 32-bit or 64-bit Java applications on the same 64-bit machine.</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2009/07/zendbroken.png"><img src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2009/07/zendbroken.png" alt="zendbroken" title="zendbroken" style="border:none" width="268" height="186" class="aligncenter size-full wp-image-2809" /></a></p>
<p>Another tidbit of interest, don&#8217;t choose a full install if you&#8217;ve already installed MySQL. The Zend Community Server isn&#8217;t smart enough to alter the configuration to another port, and their <code>my.ini</code> points to a <code>3306</code> listener port. This causes the MySQL_ZendServer51 service to fail. It also doesn&#8217;t uninstall well. If you don&#8217;t want to clean the Windows Registry, don&#8217;t choose to install a second MySQL.</p>
<p>As an FYI, the Zend installation of MySQL doesn&#8217;t put a password on the root account. Don&#8217;t forget to add one after the install if you go down the full product road. This has the <a href="http://blog.mclaughlinsoftware.com/php-programming/zend-server-installation/">Zend Server Community Edition installation instructions</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2009/07/07/zend-java-bridge-32-bit/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP OUT mode Parameter</title>
		<link>http://blog.mclaughlinsoftware.com/2009/07/03/php-out-mode-parameter/</link>
		<comments>http://blog.mclaughlinsoftware.com/2009/07/03/php-out-mode-parameter/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 03:07:50 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[OPAL]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[pl/sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=2798</guid>
		<description><![CDATA[I saw a post in the OTN forum that asked a simple question and had no simple example as an answer, so I thought it would be nice to provide one. Basically, somebody wanted to know how to call into a stored procedure and return a value with more or less one pass-by-value and another [...]]]></description>
			<content:encoded><![CDATA[<p>I saw a post in the <a href="http://forums.oracle.com/forums/thread.jspa?threadID=912546&#038;tstart=0">OTN forum that asked a simple question</a> and had no simple example as an answer, so I thought it would be nice to provide one. Basically, somebody wanted to know how to call into a stored procedure and return a value with more or less one pass-by-value and another pass-by-reference variable.</p>
<p>This defines a simple <code>echo</code> procedure, which takes a message and returns a formatted message:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">CREATE</span> <span style="color: #00F;">OR</span> <span style="color: #000;">REPLACE</span> <span style="color: #00F;">PROCEDURE</span> echo
<span style="color: #00F;">&#40;</span> message <span style="color: #00F;">IN</span>     <span style="color: #00F;">VARCHAR2</span>
<span style="color: #00F;">,</span> reply      <span style="color: #00F;">OUT</span> <span style="color: #00F;">VARCHAR2</span> <span style="color: #00F;">&#41;</span> <span style="color: #00F;">IS</span>
<span style="color: #00F;">BEGIN</span>
  reply <span style="color: #00F;">:=</span> <span style="color: #F00;">'Message ['</span><span style="color: #00F;">||</span>message<span style="color: #00F;">||</span><span style="color: #F00;">'] received.'</span><span style="color: #00F;">;</span>
<span style="color: #00F;">END</span><span style="color: #00F;">;</span>
<span style="color: #00F;">/</span></pre></td></tr></table></div>

<p>The following PHP calls the procedure and returns the value. It uses the required connection syntax for the Zend Server Community Edition.</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
23
24
25
26
27
28
29
30
31
32
33
</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;">// Attempt to connect to your database.</span>
  <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span>oci_connect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;student&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;student&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;localhost/xe&quot;</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;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Sorry! The connection to the database failed. Please try again later.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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: #666666; font-style: italic;">// Initialize incoming message whether or not parameter sent.</span>
    <span style="color: #000088;">$msg_in</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'msg'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'msg'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Cat got your keyboard?&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Set the call statement, like a SQL statement.</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;BEGIN echo(:a,:b); END;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Prepare the statement and bind the two strings.</span>
    <span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> oci_parse<span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Bind local variables into PHP statement, you need to size OUT only variables.</span>
    oci_bind_by_name<span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;:a&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$msg_in</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    oci_bind_by_name<span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;:b&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$msg_out</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span> SQLT_CHR<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Execute it and print success or failure message.</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>oci_execute<span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">print</span> <span style="color: #000088;">$msg_out</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;">print</span> <span style="color: #0000ff;">&quot;Sorry, I can't do that Dave...&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// Free resources.</span>
    oci_free_statement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$stmt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    oci_close<span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You can then test it with or without a parameter, like this example with a parameter:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">http://localhost/message_reply.php?msg=&quot;Sample message&quot;</pre></div></div>

<p>I put a link in the forum to this, and I hope it helps a few folks.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2009/07/03/php-out-mode-parameter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
