title: Why do I get a SQL error when preparing a statement in mysqli?
body: <p>I have the following query:</p>
  
  <pre><code>INSERT INTO ipi_messages (Message_userID, Message_fromName, Message_fromEmail, Message_subject, Message_body) VALUES(0, 'hope', 'thisworks@gmail.com', 'i hope', 'this works')
  </code></pre>
  
  <p>And I get the following MySQL error:</p>
  
  <pre><code>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 '0' at line 1
  </code></pre>
  
  <p>Here's my table schema:</p>
  
  <p><img src="https://i.stack.imgur.com/mWluq.png" alt="enter image description here"></p>
  
  <p>Here is my main PHP code:</p>
  
  <pre><code>&lt;?php
      require 'lib/class.Database.php';
      require 'lib/class.Messages.php';
  
      $messageDatabase = new Database('ipi2');
      $messageDatabase-&gt;newDatabaseUsers([
                                          ['Query','ipi_query','k2QvHmtxGhVN'],
                                          ['admin','ipi_admin','r0HHRsQ76kS2']
                                          ]);
      $messageDatabase-&gt;makeConnection('admin');
  
      var_dump($_POST);
  
      if(array_key_exists('sendMessage', $_POST)){
          // $query = "INSERT INTO ipi_messages (Message_userID, Message_fromName, Message_fromEmail, Message_subject, Message_body) VALUES(?,?,?,?,?)";
          // $result = $messageDatabase-&gt;query($query, $_POST['to'], $_POST['fromName'], $_POST['fromEmail'], $_POST['subject'], $_POST['message']);
  
          $messages = new Messages($messageDatabase);
          $messages-&gt;postMessage([
                                  'Message_userID' =&gt; (int)$_POST['to'],
                                  'Message_fromName' =&gt; $_POST['fromName'],
                                  'Message_fromEmail' =&gt; $_POST['fromEmail'],
                                  'Message_subject' =&gt; $_POST['subject'],
                                  'Message_body' =&gt; $_POST['message']
                                  ]);
      }
  
      if(array_key_exists('getMessages', $_POST)){
          $query_get = "SELECT * FROM ipi_messages WHERE Message_userID = ?";
          $result_get = $messageDatabase-&gt;query($query_get, $_POST['user']);
      }
  ?&gt;
  &lt;!doctype html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Messages&lt;/title&gt;
      &lt;style type="text/css"&gt;h5, h6{margin: 0;padding: 0;}&lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h3&gt;Send Message&lt;/h3&gt;
      &lt;form name="sendMessage" method="post" action=""&gt;
          &lt;h5&gt;To:&lt;/h5&gt;
          &lt;select name="to"&gt;
              &lt;option value=""&gt;&lt;/option&gt;
              &lt;option value="0"&gt;Fillip Peyton&lt;/option&gt;
              &lt;option value="1"&gt;Fillswitch&lt;/option&gt;
          &lt;/select&gt;
          &lt;br/&gt;
  
          &lt;h5&gt;Name:&lt;/h5&gt;
          &lt;input type="text" name="fromName"&gt;
          &lt;h5&gt;Email:&lt;/h5&gt;
          &lt;input type="text" name="fromEmail"&gt;
          &lt;h5&gt;Subject:&lt;/h5&gt;
          &lt;input type="text" name="subject"&gt;
          &lt;h5&gt;Message:&lt;/h5&gt;
          &lt;textarea name="message"&gt;&lt;/textarea&gt;
          &lt;br/&gt;
  
          &lt;input type="submit" name="sendMessage" value="Send"&gt;
      &lt;/form&gt;
  
      &lt;h3&gt;Your Messages:&lt;/h3&gt;
      &lt;form name="getMessages" method="post" action=""&gt;
          &lt;select name="user"&gt;
              &lt;option value=""&gt;&lt;/option&gt;
              &lt;option value="0"&gt;Fillip Peyton&lt;/option&gt;
              &lt;option value="1"&gt;Fillswitch&lt;/option&gt;
          &lt;/select&gt;
          &lt;input type="submit" name="getMessages" value="Get Messages"&gt;
          &lt;?php
              if (isset($result_get)) {
                  foreach ($result_get as $result) {
          ?&gt;
                      &lt;p&gt;
                          &lt;b&gt;&lt;?php echo $result['Message_fromName']; ?&gt;&lt;/b&gt;&lt;br/&gt;
                          &lt;?php echo $result['Message_fromEmail']; ?&gt;&lt;br/&gt;
                          &lt;i&gt;&lt;?php echo $result['Message_subject']; ?&gt;&lt;/i&gt;&lt;br/&gt;
                          &lt;?php echo $result['Message_body']; ?&gt;
                      &lt;/p&gt;&lt;hr&gt;
          &lt;?php
                  }
              }
          ?&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>class.Messages.php:</p>
  
  <pre><code>&lt;?php
  require_once 'lib/classFunctions.php';
  
  class Messages
  {
      protected $database;
      protected $userID = -1;
  
      public function __construct(Database $messageDatabase) {
          $this-&gt;database = $messageDatabase;
      }
  
      public function getMessages($userID = null){
  
      }
  
      public function postMessage(Array $columnsValues){
          // $query = "INSERT INTO ipi_messages (Message_userID, Message_fromName, Message_fromEmail, Message_subject, Message_body) VALUES(?,?,?,?,?)";
  
          $columns = '';
          $values = '';
          $queryParams = array();
  
          foreach ($columnsValues as $column =&gt; $value) {
              $columns .= "$column, ";
              $values .= is_int($value) ? "$value, " : "'$value', ";
              $queryParams[] = $value;
          }
  
          $columns = substr($columns, 0, strlen($columns) - 2);
          $values = substr($values, 0, strlen($values) - 2);
  
          $query = "INSERT INTO ipi_messages ($columns) VALUES($values)";
  
          // array_unshift($queryParams, $query);
  
          // $result = $messageDatabase-&gt;query($query, $_POST['to'], $_POST['fromName'], $_POST['fromEmail'], $_POST['subject'], $_POST['message']);
          echo $query . '&lt;br/&gt;';
          return $result = call_user_func_array(array($this-&gt;database,'query'), refValues($queryParams));
      }
  
      public function deleteMessage(){}
  }
  </code></pre>
  
  <p>class.Database.php:</p>
  
  <pre><code>&lt;?php
  require_once 'lib/classFunctions.php';
  
  class Database
  {
      protected $DATABASE = 'database';
      protected $SERVER = 'localhost';
      protected $DATABASEUSERS;
      protected $CONNECTION;
  
  
      public function __construct($database, $server = 'localhost'){
          $this-&gt;DATABASE = $database;
          $this-&gt;SERVER = $server;
      }
  
      public function makeConnection($userType){
          $user = $this-&gt;DATABASEUSERS[strtolower($userType)];
          $this-&gt;CONNECTION = new mysqli($this-&gt;SERVER, $user-&gt;getUsername(), $user-&gt;getPassword(), $this-&gt;DATABASE) or die('Cannot make connection to database...');
      }
  
      public function escapeSpecialChars($string){
          return $this-&gt;CONNECTION-&gt;real_escape_string($string);
      }
  
      public function newDatabaseUser($userType, $username, $password){
          $userType = strtolower($userType);
          $this-&gt;DATABASEUSERS[$userType] = new DatabaseUser($userType, $username, $password);
      }
  
      public function newDatabaseUsers($usersCollection){
          foreach ($usersCollection as $user) {
              $userType = strtolower($user[0]);
              $username = $user[1];
              $password = $user[2];       
  
              $this-&gt;DATABASEUSERS[$userType] = new DatabaseUser($userType, $username, $password);
          }
      }
  
      public function closeConnection(){
          if($this-&gt;CONNECTION)
              $this-&gt;CONNECTION-&gt;close();
          else
              throw new Exception("No connection available.", 1);
      }
  
      public function query($query, $vars = null){
          $stmt = $this-&gt;CONNECTION-&gt;stmt_init();
  
          $args = func_get_args();
          if($stmt-&gt;prepare($query)){
  
              if($vars != null){
                  $queryParams = array();
                  $queryTypes = '';
                  $argsCount = count($args);
  
                  for( $i = 1 ; $i &lt; $argsCount ; $i++ ){
                      $var = $args[$i];
                      $varType = gettype($var);
                      switch ($varType) {
                          case 'string':
                              $queryTypes .= 's';
                              break;
                          case 'integer':
                              $queryTypes .= 'i';
                              break;
                          case 'double':
                              $queryTypes .= 'd';
                              break;
                          case 'blob':
                              $queryTypes .= 'b';
                              break;
  
                          default:
                              throw new Exception("Could not bind parameter of type: " . $varType, 1);                        
                              break;
                      }
                  } 
                  $queryParams[] = $queryTypes;
  
                  for( $i = 1 ; $i &lt; $argsCount ; $i++ )
                      $queryParams[] = $args[$i];
  
                  // $stmt-&gt;bind_param($queryParams);
                  call_user_func_array(array($stmt,'bind_param'), refValues($queryParams));
              }
  
              $isExecuted = $stmt-&gt;execute();
              if($isExecuted)
                  return $result = $stmt-&gt;get_result();
              else
                  die("Could not execute query($query):" . $this-&gt;CONNECTION-&gt;error);
  
          }else{
              die("Could not prepare statement: " . $this-&gt;CONNECTION-&gt;error);
          }
  
          $stmt-&gt;reset();
      }
  }
  
  class DatabaseUser
  {
      protected $USERTYPE = 'query';
      protected $USERNAME = 'username';
      protected $PASSWORD = 'password';
  
      public function __construct($userType, $username, $password) {
          $this-&gt;USERTYPE = $userType;
          $this-&gt;USERNAME = $username;
          $this-&gt;PASSWORD = $password;
      }
  
      public function getUserType(){ return $this-&gt;USERTYPE; }
      public function getUsername(){ return $this-&gt;USERNAME; }
      public function getPassword(){ return $this-&gt;PASSWORD; }
  }
  </code></pre>
  
  <p>classFunctions.php:</p>
  
  <pre><code>&lt;?php
  function refValues($arr){
      if (strnatcmp(phpversion(),'5.3') &gt;= 0) //Reference is required for PHP 5.3+
      {
          $refs = array();
          foreach($arr as $key =&gt; $value)
              $refs[$key] = &amp;$arr[$key];
          return $refs;
      }
      return $arr;
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> I have added the PHP classes and scripts I am using along with updated my SQL query to make the 0 an int instead of a string.</p>
  
  <p>Why am I getting a SQL error when I prepare my mysqli statement? I tried taking just the SQL query and running it on the database, and the row inserted just fine.</p>