pdo

Cannot set current _timestamp or now() with PDO on MySQL

I use PDO prepared statements in a Web application I'm building. I have a timestamp column set to current_timestamp as default value. Using a timestamp should normally assign the current timestamp when the column value is null, same as now() would do. But no matter what I try, I get a NULL value if I set the timestamp column to null and...

Is "SET CHARACTER SET utf8" necessary?

Hi all! I´m rewritting our database class (PDO based), and got stuck at this. I´ve been taught to both use SET NAMES utf8 and SET CHARACTER SET utf8 when working with UTF-8 in PHP and MySQL. In PDO I now want to use the PDO::MYSQL_ATTR_INIT_COMMAND parameter, but it only supports one query. Is SET CHARACTER SET utf8 necessary? ...

PDO in PHP, how to improve this PDO mysql code

Thanks for checking. All helpful answers/comments are up voted. I have the following code, which does the job, but imo is not efficient. The reason I think it's not efficient is because I'm using fetchAll + loop even though I know that the query will return either 1 or no records. //assume the usual new PDO, binding, and execute are up ...

Datetime NOW PHP mysql (+ PDO variant)

Thanks for looking. All helpful answers/comments are up voted. In php, you can use NOW() like this: mysql_query("INSERT INTO tablename (id, value, time_created) VALUES ('{$id}', '{$value}', NOW())"); How can I do the same thing in PDO. When I bind like this, I get an error: $stmt->bindParam('...

PDO binding values for MySQL IN statement

Hi, I have an issue with PDO that I'd really like to get an answer for after being plagued by it for quite some time. Take this example: I am binding an array of ID's to a PDO statement for use in a MySQL IN statement. The array would be say: $values = array(1,2,3,4,5,6,7,8); The database-safe variable would be $products = implode('...

PHP PDO Prepared Insert Query. Problem with value not being passed.

I am using PDO to do prepared queries in php with MySQL. The query executes with no errors but one parameter does not pass it's value properly. It come sup as a blank field, but not NULL. It's really weird as it seems that I entered the code the same as the others which of course work fine. Really frustrating. $f_name = $_POST['f_name']...

How can I determine if a PDO statement cursor is closed?

I have a search class that keeps prepared PDO statements around for re-execution with new parameters each time the search is run. A conflict occurs if a second search is run while a previous search still has a result set open and is returning results. What I'd like to do in this case is simply create and execute a new statement rather th...

PDO + SqlAnywhere, its possible?

Hi, I would like use PHP PDO with SqlAnywhere, but don't have the driver on php site. Can I add a lib of sqlanywhere to use with PDO? ODBC is the last option. ...

Which tokens can be parameterized in PDO prepared statements?

I'm playing around with prepared statements in PHP/PDO. The basic queries work fine, passing a value to the WHERE clause: $stmt = $db->prepare( 'SELECT title FROM episode WHERE id=:id' ); $stmt->bindParam( ':id', $id, PDO::PARAM_INT ); $id = 5; $stmt->execute(); However I have a situation where I need to pass variables for the field n...

Parameter binding fails where concatenation works

I am trying to execute the following sql from php using pdo: SELECT * FROM my_table WHERE name=?. When I do this: $sql = 'SELECT * FROM my__table WHERE name=?' ; $stmt = $dbconn->prepare($sql); $stmt->bindValue(1, $_POST['name'], PDO::PARAM_STR); $stmt->execute(); I get an empty result set. When I do this: $sql = 'SELECT ...

What does Apache need to support both mysqli and PDO?

I'm considering changing some PHP code to use PDO for database access instead of mysqli (because the PDO syntax makes more sense to me and is database-agnostic). To do that, I'd need both methods to work while I'm making the changeover. My problem is this: so far, either one or the other method will crash Apache. Right now I'm using XA...

PDO Connection Class, please help me understand if I have this right.

Hi everyone, I've been playing around with PDO for the last few days, I'm working on a small CMS system to teach myself OOP skills, but even though it's only a small CMS, I want it to be able to handle whatever the web can throw at it. This is what I've come up with so far, I'm going to add connection pooling to the constructor to enab...

PDO constructor very slow (mysql)

This bit of code is taking almost a half second to execute. Could somebody help me with some reasons this could be happening and some possible solutions? If it matters, the DB is hosted by amazon rds $this->_connection = new PDO( $dsn, $this->_config['username'], $this->_config['password'], ...

Storing PDO details in php.ini

According to the PDO documentation, you can store database login details in php.ini, away from the php file (example 3). But it does not explain how to store username and password, only the database and host. How would you do that? This is the method used in the documentation: [PDO] pdo.dsn.mydb="mysql:dbname=testdb;host=localhost" I...

PDO mysql: How to know if insert was successful

I'm using PDO to insert a record (mysql and php) $stmt->bindParam(':field1', $field1, PDO::PARAM_STR); $stmt->bindParam(':field2', $field2, PDO::PARAM_STR); $stmt->execute(); Is there a way to know if it inserted successfully, for example if the record was not inserted because it was a duplicate? Edit: of course I can look at the dat...

Fetching single row, single column with PDO

I have a mysql query that targets a single column in a single row "SELECT some_col_name FROM table_name WHERE user=:user" After I execute the statement $stmt->execute(); how do I get this single cell directly placed into a variable with no loops? In other words how to get from $stmt->execute(); to $col_value = 100; I tried the 2...

reducing mysql statements that differ only in ORDER BY field name

I'm trying to sort data by different fields ascending and descending. But I have different mysql pdo statements for the 4 fields I have (8 queries total): $stmt1 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field1 DESC"); $stmt2 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field1 ASC"); $stmt3 =...

pdo binding asc/desc order dynamically

Let's say I have 2 pdo statements that differ only in order (asc vs. desc) $stmt1 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field1 DESC"); $stmt2 = $po->prepare("SELECT * FROM tabname WHERE categ=:categ ORDER BY field1 ASC"); Is there a way I can bind ASC/DESC dynamically so I can have only 1 stmt $order = "AS...

Cannot instantiate a PDO object in PHP MVC framework

Hello, Can anyone help me with this? I have this function in a singleton class. The error it is giving me back is that it cannot find the class. First I thought it had something to do with the autoloader, but I did spl_autoload_unregister('libloader') and it still gives the same error? The host is running php 5. public static functi...

PDO execute problem

I have faced a problem .I want to select all rows by executing this function: public function executeQuery($query,$fetch_mode=null) { $rs = null; if ($stmt = $this->getConnection()->prepare($query)) { if ($this->executePreparedStatement($stmt, $rs,$fetch_mode)) { return $rs; } ...