title: SQL error 1064. Statement works in mysqladmin but not in PHP script
body: <p>I have a PHP script installed on the admin side of our website that is supposed to allow us to run SQL queries.  However, I'm not getting the same results as I am when running it through mysqladmin on our host company's OPS page.</p>
  
  <p><strong>When I submit:</strong></p>
  
  <pre><code>SELECT orders_id FROM `orders_status_history` WHERE `comments` LIKE '%12345%'
  </code></pre>
  
  <p>I get one result (my test record) in mysqladmin.  So it's successful.</p>
  
  <p><strong>However, when I submit it through the PHP program, I get:</strong></p>
  
  <pre><code>MySQL error 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 '\'%12345%\'' at line 1
  
  While executing:
  
  SELECT orders_id FROM `orders_status_history` WHERE `comments` LIKE \'%12345%\'
  </code></pre>
  
  <p>I'm assuming the syntax is different when submitting through PHP but I can't, for the life of me, figure out what it's supposed to be.  I've tried prefacing the single quotes with a slash.  I've tried double quotes.  I've spent a couple hours surfing the web.  I've tried so many things I can't even keep them straight anymore.  I'm assuming it is something simple.  Can anyone point me in the right direction?</p>
  
  <p>Here is the code from the php program.  The user pastes the SQL query in a text area and hits a SEND button.  Again, the <em>exact</em> same query works in mysqladmin but not when using this PHP program:</p>
  
  <pre><code>&lt;?php
  /*
    $Id: sql_interface.php,v 1.00 2004/08/13 00:28:44 draven Exp $
  */
  
    require('includes/application_top.php');
    $text_heading = INITIAL_TITLE;
  
      function sqlquery($query) {
      $result = mysql_query($query);
      global $query_result;
        if (mysql_errno()) {
          $query_result = "MySQL error ".mysql_errno().": ".mysql_error()."\n\nWhile executing:\n\n$query\n------------------------------------------------------------------------------------------\n\n";
        } else {
          $query_result = "Your query was successful!\nRows Affected: " . mysql_affected_rows();
        }
      return $result;
    }
  
    $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
  
    if (isset($HTTP_POST_VARS['action']) &amp;&amp; ($HTTP_POST_VARS['action'] == 'process')) {
      sqlquery($HTTP_POST_VARS['query_entry']);
      $text_heading = POST_QUERY_TITLE;
      $tryagain = TRY_AGAIN_TEXT;
    }
  ?&gt;
  &lt;!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
  &lt;html &lt;?php echo HTML_PARAMS; ?&gt;&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=&lt;?php echo CHARSET; ?&gt;"&gt;
  &lt;title&gt;&lt;?php echo HEADING_TITLE; ?&gt;&lt;/title&gt;
  &lt;link rel="stylesheet" type="text/css" href="includes/stylesheet.css"&gt;
  &lt;script language="javascript" src="includes/general.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF"&gt;
  &lt;!-- header //--&gt;
  &lt;?php require(DIR_WS_INCLUDES . 'header.php'); ?&gt;
  &lt;!-- header_eof //--&gt;
  
  &lt;!-- body //--&gt;
  &lt;table border="0" width="100%" cellspacing="2" cellpadding="2"&gt;
    &lt;tr&gt;
      &lt;td width="&lt;?php echo BOX_WIDTH; ?&gt;" valign="top"&gt;&lt;table border="0" width="&lt;?php echo BOX_WIDTH; ?&gt;" cellspacing="1" cellpadding="1" class="columnLeft"&gt;
  &lt;!-- left_navigation //--&gt;
  &lt;?php require(DIR_WS_INCLUDES . 'column_left.php'); ?&gt;
  &lt;!-- left_navigation_eof //--&gt;
      &lt;/table&gt;&lt;/td&gt;
  &lt;!-- body_text //--&gt;
      &lt;td width="100%" valign="top"&gt;&lt;table border="0" width="100%" cellspacing="0" cellpadding="2"&gt;&lt;?php echo tep_draw_form('sql_interface', 'sql_interface.php', 'post') . tep_draw_hidden_field('action', 'process'); ?&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;table border="0" width="100%" cellspacing="0" cellpadding="0"&gt;
            &lt;tr&gt;
              &lt;td class="pageHeading" colspan="3"&gt;&lt;?php echo HEADING_TITLE; ?&gt;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/table&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;table border="0" width="100%" cellspacing="0" cellpadding="0"&gt;
            &lt;tr&gt;
              &lt;td valign="top"&gt;&lt;table border="0" width="100%" cellspacing="0" cellpadding="2"&gt;
                    &lt;tr&gt;
                      &lt;td class="main" colspan="3"&gt;&lt;?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                      &lt;td class="main" colspan="2"&gt;&lt;?php echo '&lt;b&gt;' . $text_heading . ':&lt;/b&gt;'; ?&gt;&lt;/td&gt;
                      &lt;td class="main" align="right" colspan="1"&gt;&lt;i&gt;&lt;?php echo $tryagain; ?&gt;&lt;/i&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                      &lt;td class="main" colspan="3"&gt;&lt;?php echo tep_draw_textarea_field('query_entry', '', 137, 30, $query_result, '', false); ?&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                      &lt;td class="main" colspan="3"&gt;&lt;?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td width="10"&gt;&lt;?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?&gt;&lt;/td&gt;
                  &lt;td colspan="2"align="right"&gt;&lt;?php echo tep_image_submit('button_send.gif', IMAGE_BUTTON_EXECUTE_SQL) . tep_draw_separator('pixel_trans.gif', '10', '1'); ?&gt;&lt;/form&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td class="smallText" colspan="3"&gt;&amp;nbsp;&lt;/td&gt;
                &lt;/tr&gt;
              &lt;/table&gt;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/table&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/table&gt;&lt;/td&gt;
  &lt;!-- body_text_eof //--&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  &lt;!-- body_eof //--&gt;
  
  &lt;!-- footer //--&gt;
  &lt;?php require(DIR_WS_INCLUDES . 'footer.php'); ?&gt;
  &lt;!-- footer_eof //--&gt;
  &lt;br&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?&gt;
  </code></pre>