Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
PHP PDO Not Binding Values
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="GarettM" data-source="post: 406259" data-attributes="member: 839"><p>Hey i am trying to bind values to a query, when i manually "bind" it works just fine but when i try to do it another way "Way #2" it doesn't work <img src="/styles/default/xenforo/smilies/emojione/frown.png" class="smilie" loading="lazy" alt=":(" title="Frown :(" data-shortname=":(" /></p><p>Manual Query Function</p><p>[PHP]</p><p>public function query($sql, $params = array())</p><p>{</p><p> $this->statement = $this->pdo->prepare($sql, $params);</p><p> $this->statement->execute();</p><p>}</p><p></p><p>$this->result('SELECT value FROM cms_settings WHERE key=:key', array(':key' => 'url')); # returns http://localhost</p><p>[/PHP]</p><p></p><p>Trying to clean it up but it doesn't work / Way #2</p><p>[PHP]</p><p>public function query($sql, $params = array)</p><p>{</p><p> $this->statement = $this->pdo->prepare($sql);</p><p></p><p> foreach($params as $key => $value)</p><p> $this->statement->bindValue(sprintf(':%s', $key), $this->quote($value));</p><p></p><p> $this->statement->execute();</p><p>}</p><p></p><p>$this->result('SELECT value FROM cms_settings WHERE key=:key', array('key', 'url')); # returns false</p><p>[/PHP]</p><p></p><p>Common Function</p><p>[PHP]</p><p>public function result($sql, $params = array())</p><p>{</p><p> $this->query($sql, $params);</p><p> $result = $this->statement->fetch(PDO::FETCH_ASSOC);</p><p> if(is_array($result))</p><p> array_shift($result);</p><p> return $result;</p><p>}</p><p></p><p>private function quote($value)</p><p>{</p><p> return sprintf("'%s'", $value);</p><p>}</p><p>[/PHP]</p></blockquote><p></p>
[QUOTE="GarettM, post: 406259, member: 839"] Hey i am trying to bind values to a query, when i manually "bind" it works just fine but when i try to do it another way "Way #2" it doesn't work :( Manual Query Function [PHP] public function query($sql, $params = array()) { $this->statement = $this->pdo->prepare($sql, $params); $this->statement->execute(); } $this->result('SELECT value FROM cms_settings WHERE key=:key', array(':key' => 'url')); # returns http://localhost [/PHP] Trying to clean it up but it doesn't work / Way #2 [PHP] public function query($sql, $params = array) { $this->statement = $this->pdo->prepare($sql); foreach($params as $key => $value) $this->statement->bindValue(sprintf(':%s', $key), $this->quote($value)); $this->statement->execute(); } $this->result('SELECT value FROM cms_settings WHERE key=:key', array('key', 'url')); # returns false [/PHP] Common Function [PHP] public function result($sql, $params = array()) { $this->query($sql, $params); $result = $this->statement->fetch(PDO::FETCH_ASSOC); if(is_array($result)) array_shift($result); return $result; } private function quote($value) { return sprintf("'%s'", $value); } [/PHP] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
PHP PDO Not Binding Values
Top