title: Why does my PDO statement -> execute return false?
body: <p>After near endless rounds of testing different aspects of this, I've determined that the PDO connection works (I can run a simple query and display results), I've determined that the statement is successfully preparing, and that the values are binding properly. For some reason, the statement won't execute. Just to be cute, I've tried removing all bound variables and executing a static query, and that won't work either.</p>
  
  <p>Code:</p>
  
  <pre><code>$dbh = new PDO( "mysql:host=localhost;dbname=".$GLOBALS['data_name'], $GLOBALS['data_user'], $GLOBALS['data_pass'] );
  $dbh-&gt;setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  
  $sth = $dbh-&gt;prepare( "SELECT * FROM :table WHERE :field = :value" );
  if( $sth != false ) TCDebug( 'prepared' );
  if( $sth-&gt;bindValue( ":table", $table ) ) TCDebug( "table true" );
  if( $sth-&gt;bindValue( ":field", $field ) ) TCDebug( "field true" );
  if( $sth-&gt;bindValue( ":value", $value ) ) TCDebug( "value true" );
  $flag = $sth-&gt;execute();
  
  if( $flag === true ) {
      TCDebug( 'flag = true' );
  } else if( $flag === false ) {
      TCDebug( 'flag = false' );
  }
  $result = $sth-&gt;fetchAll();
  
  foreach( $result as $c ) TCDebugArr( $c );
  TCDebug( count( $result ) );
  if( count( $result ) &gt; 0 ) {
      return $result;
  } else {
      return null;
  }
  </code></pre>
  
  <p>Consistently echos debug text of 'prepared' 'table true' 'field true' 'value true' 'flag = false' which tells me that preparing and binding work, but executing doesn't, $result is empty and the function returns null.</p>
  
  <p>I've probably overlooked something horrendously obvious, and I'm fully prepared to hang my head in utter n00b shame. Thank you in advance...</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Ahh, concatenation -- my friend today. Working code follows:</p>
  
  <pre><code>$dbh = new PDO( "mysql:host=localhost;dbname=".$GLOBALS['data_name'], $GLOBALS['data_user'], $GLOBALS['data_pass'] );
  $dbh-&gt;setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  
  $prepare_str = "SELECT * FROM ". $table ." WHERE ". $field ." = :value";
  
  $sth = $dbh-&gt;prepare( $prepare_str );
  if( $sth != false ) TCDebug( 'prepared' );
  if( $sth-&gt;bindValue( ":value", $value ) ) TCDebug( "value true" );
  $flag = $sth-&gt;execute();
  
  if( $flag === true ) {
      TCDebug( 'flag = true' );
  } else if( $flag === false ) {
      TCDebug( 'flag = false' );
  }
  $result = $sth-&gt;fetchAll();
  
  foreach( $result as $c ) TCDebugArr( $c );
  TCDebug( count( $result ) );
  if( count( $result ) &gt; 0 ) {
      return $result;
  } else {
      return null;
  }
  </code></pre>
  
  <p>This is safe in this instance, since <code>$table</code> and <code>$field</code> are system-generated and in no way accessible via user input; only $value is exposed.</p>
  
  <p>Thank you StackOverflow! You're my favorite! :)</p>