Note that there are some explanatory texts on larger screens.

plurals
  1. POIncluding $variable in MySQL
    text
    copied!<p>Here are my failed attempts to include a PHP variable in a MySQL expression. Replacing the variable with a 1 results in the results being printed. Any help will be appreciated.</p> <pre><code> $query = " SELECT name FROM teams WHERE id = '$shooterID'"; $shooters = mysql_query($query) or die(mysql_error()); $i = 0; while($shooter = mysql_fetch_array( $shooters )) { echo $shooter[$i]; $i++; } </code></pre> <hr> <pre><code>$shooters = mysql_query(" SELECT name FROM teams WHERE id = '$shooterID'") or die(mysql_error()); $i = 0; while($shooter = mysql_fetch_array( $shooters )) { echo $shooter[$i]; $i++; } </code></pre> <p>Thanks</p> <hr> <p>Attempting to utilize the methods here have not fully solved the problem (though thanks again). Here are my revised efforts along with further context (I don't need to sanitize the data as it is coming directly from another query.</p> <pre><code>$shooters = mysql_query(" SELECT * FROM events JOIN teams on events.shooter = teams.id ") or die(mysql_error()); $i = 0; while($results = mysql_fetch_array( $shooters )) { $shooterIDs[$i] = $results[0]; $i++; } //var_dump($shooterIDs); == array(1) { [0]=&gt; string(1) "1" } $query = " SELECT name FROM teams WHERE id = '".$shooterID[0]."'"; $shooters = mysql_query($query) or die(mysql_error()); while($shooter = mysql_fetch_array( $shooters )) { echo $shooter[0]; } </code></pre> <hr> <p>Turns out my last attempt was missing a 's' in the variable namee $shooterIDs[0]. Stupid error. There were probably others as well that have been already solved with all of your help. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload