title: How to escape apostrophe in php variable for select query
body: <p>I have tried escaping the query string and the <code>$variable</code> containing the apostrophe using the mysqli_real_escape_string the variable value is coming form the database. I am getting the following error.</p>
  
  <blockquote>
    <p>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 '\'Shamrock Rovers%\' AND away_team like \'St Patrick's Athletic%\'' at line 1</p>
  </blockquote>
  
  <p>The apostrophe is not getting escaped by the quotes around the comparison values is.</p>
  
  <p>Here is the query as it appears in the PHP file:</p>
  
  <pre><code>    $homeTeam = filter_input(INPUT_GET, 'homeTeam', FILTER_SANITIZE_STRING);
      $homePlayers = "select * from players where team_name like $homeTeam";
      $homePlayers = mysqli_real_escape_string($dbc, $homePlayers);
      $homePlayersResult = mysqli_query($dbc, $homePlayers);
  </code></pre>
  
  <p>And <code>echo</code>ed out to the browser:</p>
  
  <pre><code>    select * from players where team_name like Shamrock Rovers
  </code></pre>
  
  <p>I have tried it a number of different ways with no variation in results i feel i am overlooking something simple. 
  Thanks in advance.</p>
  
  <p><strong>Edit 1</strong></p>
  
  <p><strong><em>Updated Code</em></strong></p>
  
  <pre><code>$homeTeam = filter_input(INPUT_GET, 'homeTeam', FILTER_SANITIZE_STRING);          
  $homeTeam = mysqli_real_escape_string($dbc, $homeTeam);
  echo "&lt;br&gt;".$homeTeam."&lt;br&gt;";
  $homePlayers = "select * from players where team_name like '$homeTeam%'";
  
  $homePlayersResult = mysqli_query($dbc, $homePlayers);
  </code></pre>
  
  <p>This Script recieves 3 parameters from a processing script</p>
  
  <pre><code>header("location: ../scorer.php?gameWeek=$gameWeek&amp;homeTeam=$homeTeam&amp;awayTeam=$awayTeam");
  </code></pre>
  
  <p>outputs
       select * from players where team_name like 'St Patrick's Athletic%'</p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>after entering the query in to mysql command window nothing happens when i submit the query once but when i enter it again i get the following err.
  <img src="https://i.stack.imgur.com/imfg8.png" alt="enter image description here"></p>