Note that there are some explanatory texts on larger screens.

plurals
  1. POSQLSTATE[42000]: Syntax error or access violation: 1064
    primarykey
    data
    text
    <p>I am writing some software right now. Reader's digest version: users select a package, enter their name, email, and desired subdomain. Subdomain is then checked to see if someone already registered it, and if it is only alphanumeric. I had all of this working using OO mysqli, but I have decided to make the move to PDO.</p> <p>Exact wording of the error: </p> <blockquote> <p>SQLSTATE[42000]: Syntax error or access violation: 1064 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 '->subdomain' at line 1</p> </blockquote> <p>When I get to instantiating my Admin object, all is well. However, when I call the <code>createAccount()</code> function, all hell breaks loose. The stack trace is all over the place, and I can barely figure out when to begin troubleshooting this. I have checked the other answers here, and they all seem too localized, so here's the code that produces it, then all of the methods that contain errors. Here we go....</p> <p>First, the code that produces the error:</p> <pre><code>include 'classes/Admin.class.php'; $admin = new Admin('test@test.com'); try { $admin-&gt;createAccount('John', 'Smith', 'test', 'pro'); } catch(Exception $e) { echo '&lt;pre /&gt;'; echo $e-&gt;getMessage(); die(print_r($e-&gt;getTrace())); } </code></pre> <p>Admin Class Constructor</p> <pre><code>public function __construct($email) { $this-&gt;email = $email; include 'Database.class.php'; include 'PasswordHash.php'; define("DB_HOST", "localhost"); define("DB_USER", "root"); define("DB_PASS", ""); define("DB_NAME", "ems"); $this-&gt;data = new Database; $this-&gt;hasher = new PasswordHash(8, false); } </code></pre> <p>Inside Admin class,checking the subdomain</p> <pre><code>private function checkSubdomain() { $this-&gt;data-&gt;query('SELECT * FROM clients WHERE subdomain = :subdomain'); $this-&gt;data-&gt;bind(':subdomain', $this-&gt;subdomain); $this-&gt;data-&gt;execute(); return ($this-&gt;data-&gt;rowCount() == 0) &amp;&amp; (ctype_alnum($this-&gt;subdomain)); } </code></pre> <p>PDO class execute, bind, and query</p> <pre><code>public function execute() { return $this-&gt;stmt-&gt;execute(); } public function query($query) { $this-&gt;stmt = $this-&gt;dbh-&gt;prepare($query); } public function bind($param, $value, $type = null) { if(is_null($type)) { switch(true) { case is_int($value): $type = PDO::PARAM_INT; break; case is_bool($value): $type = PDO::PARAM_BOOL; break; case is_null($value): $type = PDO::PARAM_NULL; default: $type = PDO::PARAM_STR; } } $this-&gt;stmt-&gt;bindValue($param, $value, $type); } </code></pre> <p>This error is rampant through my code, so I think its only in the PDO class, but my Session class worked just fine. Also, my queries worked perfectly using only mysqli, so I am confident in my syntax. Any help is greatly appreciated.</p> <p>By request, the create account function:</p> <pre><code>public function createAccount($firstname, $lastname, $subdomain, $package) { $this-&gt;firstname = $firstname; $this-&gt;lastname = $lastname; $this-&gt;subdomain = $subdomain; $this-&gt;package = $package; //does the subdomain exist, or is it invalid? if(!$this-&gt;checkSubdomain()) throw new Exception('This domain is not available. It can only contain letters and numbers and must not already be taken.'); //now comes the table creation, provided everything is in order $this-&gt;setup(); } </code></pre> <p>Here's the setup function that is called by createAccount (sans all of the table structures):</p> <pre><code>private function setup() { $isError = false; $queries = array( //all of these are CREATE statements, removed for security reasons ); //need a database that matches with the client subdomain $this-&gt;data-&gt;query('CREATE TABLE $this-&gt;subdomain'); $this-&gt;data-&gt;bind(':subdomain', $this-&gt;subdomain); //PDO secured...execute $this-&gt;data-&gt;execute(); $this-&gt;data-&gt;beginTransaction(); foreach($queries as $query) { $this-&gt;data-&gt;query($query); if(!$this-&gt;data-&gt;execute()) { //we hit some sort of error, time to GTFO of here $isError = true; $this-&gt;data-&gt;cancelTransaction(); //die('Error with: ' . $query); break; } } if(!$isError) { $this-&gt;data-&gt;endTransaction(); mkdir('a/' . $this-&gt;subdomain, 0755); $this-&gt;generatePass(); //this is where I insert the user into the admin table using PDO, removed for security $this-&gt;data-&gt;execute(); } $this-&gt;data-&gt;close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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