<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Usage</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-memcache.quickstart.configuration.html">Setup</a></div> <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-memcache.setup.html">Installing/Configuring</a></div> <div class="up"><a href="mysqlnd-memcache.quickstart.html">Quickstart and Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mysqlnd-memcache.quickstart.usage" class="section"> <h2 class="title">Usage</h2> <p class="para"> After associating a MySQL connection with a Memcache connection using <span class="function"><strong>mysqnd_memcache_set()</strong></span> the plugin attempts to transparently replace SQL <em>SELECT</em> statements by a memcache access. For that purpose the plugin monitors all SQL statements executed and tries to match the statement string against <strong><code>MYSQLND_MEMCACHE_DEFAULT_REGEXP</code></strong>. In case of a match, the mysqlnd memcache plugin checks whether the <em>SELECT</em> is accessing only columns of a mapped table and the <em>WHERE</em> clause is limited to a single key lookup. </p> <p class="para"> In case of the example SQL table, the plugin will use the Memcache interface of the MySQL server to fetch results for a SQL query like <em>SELECT f1, f2, f3 WHERE id = n</em>. </p> <p class="para"> <div class="example" id="example-1933"> <p><strong>Example #1 Basic example.</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"host"</span><span style="color: #007700">, </span><span style="color: #DD0000">"user"</span><span style="color: #007700">, </span><span style="color: #DD0000">"passwd"</span><span style="color: #007700">, </span><span style="color: #DD0000">"database"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$memc </span><span style="color: #007700">= new </span><span style="color: #0000BB">Memcached</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$memc</span><span style="color: #007700">-></span><span style="color: #0000BB">addServer</span><span style="color: #007700">(</span><span style="color: #DD0000">"host"</span><span style="color: #007700">, </span><span style="color: #0000BB">11211</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">mysqlnd_memcache_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">, </span><span style="color: #0000BB">$memc</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*<br /> This is a query which queries table test using id as key in the WHERE part<br /> and is accessing fields f1, f2 and f3. Therefore, mysqlnd_memcache<br /> will intercept it and route it via memcache.<br />*/<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT f1, f2, f3 FROM test WHERE id = 1"</span><span style="color: #007700">);<br />while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">$result</span><span style="color: #007700">-></span><span style="color: #0000BB">fetch_row</span><span style="color: #007700">()) {<br /> </span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*<br /> This is a query which queries table test but using f1 in the WHERE clause.<br /> Therefore, mysqlnd_memcache can't intercept it. This will be executed<br /> using the MySQL protocol<br />*/<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT id FROM test WHERE f1 = 'Lady'"</span><span style="color: #007700">);<br />while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">$result</span><span style="color: #007700">-></span><span style="color: #0000BB">fetch_row</span><span style="color: #007700">()) {<br /> </span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array( [f1] => Hello [f2] => World [f3] => ! ) array( [id] => 2 ) </pre></div> </div> </div> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-memcache.quickstart.configuration.html">Setup</a></div> <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-memcache.setup.html">Installing/Configuring</a></div> <div class="up"><a href="mysqlnd-memcache.quickstart.html">Quickstart and Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>