pdo

Understand PDO fetch and fetchAll iteration with foreach

With this: private function jsonArray($object) { $json = array(); if(isset($object) && !empty($object)) { foreach($object as $obj) { ... } } return $json; } We iterate over an object with success. We use PDO::fetch[FETCH_OBJ] and it works. What if, we want to iterate over an array of objects like the on...

Getting insert id with insert PDO MySQL

Im getting to grips with the basics of PDO. However Im trying to get the id of the inserted row, Im using: $query = $system->db->prepare("INSERT INTO {$this->_table} (name,description) VALUES (:name,:description)"); $query->execute(array('name'=>$name,'description'=>$description)); The tutorials I have come across are regarding trans...

PDO prepare statements: Do we need to escape?

public function receiveDomainNames($keyword) { try { $stmt = $this->_dbh->prepare("SELECT d.someField FROM domain d WHERE d.someField LIKE :keyword"); $someField = '%'.$keyword.'%'; Do we need to escape $keyword on this case? On php manual we can read: If an application exclusively uses prepared statements, the develop...

Retrieve (or simulate) full query from PDO prepared statement

I stumbled upon this question from two years ago. Is there a way to get the raw SQL string executed when calling PDOStatement::execute() on a prepared statement? For debugging purposes this would be extremely useful. The winning answer states that [...] You can also get what you want if you set the PDO attribute PDO::ATTR_...

why does extending PDO causes me a memory overflow?

i have a class that extends the PDO class. it's called Database. but in a particular function, the commit() function, it gets an memory overflow error. Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261900 bytes) in C:\wamp\www\igadgets\application\includes\base\classes\database.php on line 130 the fu...

PDO bindValues isn't binding my values

I can't get bindValue to bind my values to the sql query. $sql = "INSERT INTO :table(:columns) VALUES(:values)"; $query = $conn->prepare($sql); $query->bindValue(':table',$table); $query->bindValue(':columns',$columns); $query->bindValue(':values',$values); $query->execute(); When I run this, $query->execute() returns "false" an...

Insert/update helper function using PDO

I have a very simple helper function to produce SET statement for traditional plain mysql driver usage: function dbSet($fields) { $set=''; foreach ($fields as $field) { if (isset($_POST[$field])) { $set.="`$field`='".mysql_real_escape_string($_POST[$field])."', "; } } return substr($set, 0, -2); } used like this...

PHP - PDO mysql backup to remote host?

Welcome, How can i create a full backup of my database (all tables inside) to remote mysql server using PHP PDO ? Is there any easy way ? ...

Use both PDO prepared statemnts and filter_var?

Hey guys, I'm learning OO PHP, and have been looking into PDO -- One thing I'm not clear on though is whether I should be using PDO prepared statements in conjunction with the filter_var() function or just by themselves. For instance, should I be doing $query = $database->connection->prepare("SELECT name FROM acounts WHERE id = :id LIMI...

mysql PDO SET statement encounter errors

I was looking for something that is related to a mysql query and I stumbled to this link mysql variable usage and saw the stored proc-like variable setting. I've tried it in phpmyadmin : SET @value = 5; SELECT @rank; and it is working, but when I tried to place it in PDO statements, it does not work. $value = "SET @value = 0"; ...

PHP + PDO + Oracle: Proxy User

Is there anyway to connect on oracle by a proxy user using PDO? ...

PEAR DB prepare/bind values

Just can't seem to print the binded values without executing the query. Looking to debug the query before execution. Any tips? I know I'm overlooking something simple, ugh... $field1 = 'one'; $field2 = 'two'; $field3 = 'three'; $fields = 'SET '; $fields .= 'field1 = ?, '; $fields .= 'field2 = ?, '; $fields .= 'field3 = ? '; $vals[] =...

Most efficient way to populate array from database values?

If I wanted to get a list of product_ids with a certain brand. I would do this: $id_list = array(); $qry = 'SELECT product_id FROM products WHERE product_brand = :brand'; $STH = $this->pdo->prepare($qry); $STH->execute(array("brand" => $brand)); $STH->setFetchMode(PDO::FETCH_ASSOC); while($row = $STH->fetch()) { $id_list[] = $row['p...

PHP PDO obj works on PHP CLI, but not when page is accessed

As a response to my previous question, I think I may have discovered which part isn't working correctly. I have a small section of PHP code which uses a PDO object to add to a sqlite3 database that is used in a AJAX call. When this code is executed using the php cli by issuing the command: "php add.php" everything works as expected and...

pdo fetch within a fetch error

I am trying to run the following script (unsuccessfully): $variables = $db->query("SELECT * FROM table1 WHERE Session_ID = '$sess1'"); while($row = $variables->fetch()) { //FETCH DATA $id= $row["ID"]; $info = $db->query("SELECT * FROM table2 WHERE ID = $id"); while($row2 = $info->fetch()) { $name = $row2["FNAME"]." ".$row2["LNAM...

MySQL Issue Copying from one table into another directly

I use the following code to copy from one table directly into another: $transfer = $db->exec("INSERT INTO table2 SELECT * FROM table1 WHERE groupname = '$gname'"); The issue I have, however, is the ID field of both tables do not necessarily match (both auto-increment) and at times this may mean one temporary's tables ID# is higher than...

PDO FETCH_INTO Factory?

I have a factory called ProductFactory I can create a product like so: $product = ProductFactory::getProduct($id); Now depending on what type of product we are getting, this function may return a class of type Product or a descendant or Product. But using the method above, the $product class would be responsible for connecting to th...

PDO insert error

I'm trying to get the code below to work..... The error: ERRORSQLSTATE[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 '?, ?, ?, ?)' at line 1 The code: $data=array($idApplications,$author,$addedOn,...

pdo select statement returning no rows

I am making a simple select from mySQL using PHP. I am doing something wrong that I can't seem to track down. Here's my statement: $storyTitle = $_GET['title']; $storyDate = urldecode($_GET['date']); $SQL = "SELECT * FROM tblContent WHERE REPLACE(contentTitle,' ' , '-') = :storyTitle AND date...

Best way to remove the trace information from the PDOException __toString method

Is it possible to remove the trace elements from the __toString. What I want is something like this. class DBException extends PDOException { public function __toString() { return get_class($this) . " '{$this->getMessage()}' in {$this->getFile()}({$this->getLine()})\n"; } } I've tried the above method but it doesn't seem to work....