title: Active record query failed - Escape quote from query
body: <p><strong>Background</strong></p>
  
  <ul>
  <li>Framework: Codeignighter/PyroCMS</li>
  </ul>
  
  <p>I have a DB that stores a list of products, I have a <code>duplicate function</code> in my application that first looks for the common product name so it can add a '<code>suffix</code>' value to the duplicated product.</p>
  
  <p><strong>Code in my Products model class</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>$product = $this-&gt;get($id);
  
  $count = $this-&gt;db-&gt;like('name', $product-&gt;name)-&gt;get('products')-&gt;num_rows();
  
  $new_product-&gt;name = $product-&gt;name . ' - ' . $count;
  </code></pre>
  
  <p>On the second line the application fails only when the <code>$product-&gt;name</code> contains quotes.
  I was with the understanding that Codeignighter escaped all strings so I dont know why I get this error.</p>
  
  <p>So I tried to use MySQL escape string function but that didn't help either.</p>
  
  <p><strong>The Error Message</strong></p>
  
  <pre class="lang-none prettyprint-override"><code>A Database Error Occurred
  
  Error Number: 1064
  
  You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Book%'' at line 3
  
  SELECT * FROM `products` WHERE `name` LIKE '%Harry\\'s Book%'
  </code></pre>
  
  <p><strong>var_dump</strong></p>
  
  <p>Below is the output of doing a <code>var_dump</code> on <code>product-&gt;name</code> before and after the line in question;</p>
  
  <pre><code>string 'Harry's Book' (length=12)
  
  A Database Error Occurred
  
  Error Number: 1064
  
  You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Book%'' at line 3
  
  SELECT * FROM `products` WHERE `name` LIKE '%Harry\\'s Book%'
  </code></pre>