title: Using a PHP Variable in a MySQL Query
body: <p>So I have the following MySQL query that I want to execute through a PHP script:</p>
  
  <pre><code>for($y=0;$y&lt;$usercount;$y++){
    $findweight = "replace into weight (user_id, column1) 
      select user_id,
      0.4*column1_a+0.6*column1_bon as calc1
      from history_summary where user_id=$y";
  
    if (mysqli_query($link, $findweight)) {
      echo "success &lt;br&gt;";
    } else {
      echo "Error: " . $sql . "&lt;br&gt;" . mysqli_error($link);
    }
  }
  </code></pre>
  
  <p>But somehow everytime I execute the script it fails to work. Everytime the script is executed, the text "success" is outputted, which means that the MySQL query should have been successfully executed, but when I check my database, nothing seems to have changed. Yet when I run the query directly such as the following:</p>
  
  <pre><code>replace into weight (user_id, column1) 
  select user_id,
  0.4*column1_a+0.6*column1_bon as calc1
  from history_summary where user_id=1
  </code></pre>
  
  <p>It works just fine. I'm assuming the issue is that the variable $y failed to be passed on properly to the query, but I'm not quite sure. I've tried the other solutions posted on this site but none of them seem to work. If anyone can help out that'd be great!</p>