title: MySQL/PHP LIKE with percent signs not working
body: <p>I am aware that this question has been asked several times before, but I am unable to find the error of my ways. </p>
  
  <p>The question: mysql_num_rows is returning false results when </p>
  
  <pre><code>$sql = "SELECT * FROM $topic WHERE $names LIKE '%$q%'";
  </code></pre>
  
  <p>But if I replace $sql with any of the following, it will return true.</p>
  
  <pre><code>$sql = "SELECT * FROM $topic WHERE $names LIKE '%j%'";
  $sql = "SELECT * FROM $topic WHERE $names ='Jack'";
  $sql = "SELECT * FROM $topic WHERE $names = '$q' ";
  </code></pre>
  
  <p>Results of var_dump </p>
  
  <pre><code>string(8) "Cust_Reg" string(5) "fName" string(2) "j " 
  resource(8) of type (mysql result) 
  string(46) "SELECT * FROM Cust_Reg WHERE fName LIKE '%j %'"
  </code></pre>
  
  <p>If I change $sql = "SELECT * FROM $topic WHERE $names LIKE '%j%'";</p>
  
  <p>var_dump for $row['ID'] will display </p>
  
  <pre><code>"SELECT * FROM Cust_Reg WHERE fName LIKE '%j%'" string(4) "NjA=" 
  string(4) "NjE=" string(4) "NjQ=" string(4) "NjY=" string(4) "ODI="
  </code></pre>
  
  <p>If you could correct me in the error of my way, I will apprecaite it. </p>
  
  <pre><code>    $q = mysql_real_escape_string($_GET['search']);
      $q = strtolower($q);
      $topic = mysql_real_escape_string($_GET['test']);
      $names = mysql_real_escape_string($_GET['name']);
  
      // SELECT * from Account_Reg where Account_Name LIKE '%$q%'
      $table = "&lt;table style='width:400px; padding:10; display:block;'&gt;&lt;tbody&gt;            
                  &lt;tr&gt;&lt;td&gt;ID&lt;/td&gt;&lt;td&gt;Account&lt;/td&gt;&lt;td&gt;First Name&lt;/td&gt;&lt;td&gt;Email&lt;/td&gt;&lt;/tr&gt;"; 
  
          $sql = "SELECT * FROM $topic WHERE $names LIKE '%{$q}%'";
          $result = mysql_query($sql) or die (mysql_error());
  
          var_dump($topic);
          var_dump($names);
          var_dump($q);   
          var_dump($result);
          var_dump($sql); 
  
      if(is_resource($result) &amp;&amp; mysql_num_rows($result) &gt; 0){
  
          while($row = mysql_fetch_array($result)) {                              
              $table .= "&lt;tr&gt;&lt;td&gt;".$row['ID']."&lt;/td&gt;";
              $cryt = base64_encode($row['ID']);
              $row['ID'] = htmlspecialchars($cryt);
              $link = "profile.cust.update.php?id=". urlencode($row['ID']);
              $link = htmlentities($link);    
  
              if($names == "fName"){      
                  $name = $row['fName'];
  
              }elseif($names == "Account_Name"){
                  $name = $row['Account_Name'];
                  $row['email_add'] = "";     
              }   
  
              $table .="&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;a href='" .$link ."'&lt;/a&gt;"  .$name."&lt;/td&gt;&lt;td&gt;".$row['email_add']."&lt;/td&gt;&lt;/a&gt; &lt;/tr&gt;";
          }$table .="&lt;/tbody&lt;/table";
    }else{$table = "No row is selected"; }
  </code></pre>