pdo

Would Singleton be a good design pattern for a microblogging site?

I had not done with any OO in the past in projects, as I kept it simpler (in fact using archaic mysql_query calls and my own filtering) so I wanted to start a new project, learning to use design patterns with my OO along the way. I was looking to build a microblogging site for kicks, and found the Singleton design pattern class which se...

PHP: Custom error handler for PDO?

I am attempting to try to do something useful with PDO exceptions other than display them, but I cannot find for the life of me how to use an error handler (set_error_handler) or anything custom to deal with PDOs exceptions. Now I am using try..catch blocks of course the catch the exception, do I implement a custom error handler in the...

Is there a way to make a param optional in PHP PDO?

Say I have this query: $qry = 'SELECT p.id FROM products p WHERE p.type = :type AND p.parent = :parent'; If I wanted to make "type" optional then the only way I know how to do it would be to do something like this: $qry = 'SELECT p.id FROM products p WHERE p.parent = :parent'; if(isset($type)) { $qry .= 'AND p....

Executing a prepared PDO statement with the like clause

I am new to PHP, and am trying to learn to use PDO to connect to a test MySQL db. I have the following: try { $db = new PDO('mysql:dbname=MYDBNAME;host=MYHOST', 'USERNAME', 'PASSWORD'); $query = "select * from books where ? like '%?%'"; $stmt = $db->prepare($query); $stmt->execute(array($searchtype, $searchterm)); } c...

Is is possible to set a default PDO fetch mode?

Before I retrieve data I always have to type: $STH->setFetchMode(PDO::FETCH_OBJ); In the interest of making my code more readable it would be great if I could set a default mode somewhere.... Thanks! Edit. I was originally hoping I could add PDO:FETCH_OBJ to the setAttribute code I run when I connect to the DB, but that doesn't seem...

PDO prepare Insert serialize data error

I am trying to insert a serialize data into mySQL using PDO and I'm hitting some syntax error. Have I missed out something? Some simplified coding: $test['1'] = "one"; $condition = serialize($test); $stmt = $dbh->prepare("INSERT INTO weather(condition) VALUES (:condition)"); $stmt->bindParam(":condition",$condition); $stmt->execute(); ...

PHP escaping encoded quotes

I have following piece of code which posts some data into database: $post = trim( $post ); $post = htmlentities( $post, ENT_QUOTES, null, false ); $statement = "INSERT INTO table (row) VALUES (:message)"; $prepared_posts = $pdo->prepare( $statement ); $prepared_posts->execute( array( ':message' => $post ) ); I have MySQL version 5.1....

PDO FreeTDS error

I have just upgraded my server from php 5.2 to 5.3.3 and I am having a weird error in PDO (with DBLIB). I am connecting to an SQL Server 2005. I am having The error appears in any statement that prefixes a parameter with the "N" letter (to tell SQL Server that it's a unicode string). The query looks like this: INSERT INTO IPs (IP, Pos...

PHP/PDO: return query rows without foreach?

I am confused about this, I run a query such as foreach($dbh->query("SELECT * FROM ...") as $row) { ...do something with row() But when I var_dump $dbh it is a PDOstatement object. This leads me to two questions: How does foreach somehow resolve the object into separate rows? How can I store all rows in an array, like $arr = $d...

underscore in php db variable causing problems

I store all of my login information for databases in files outside of the public tree in variables such as $hostname = '172.0.0.0'; $dbname = 'myname_mydbname'; $username = 'myname_user'; $pw = 'password'; That's pretty standard. The problem is that this particular hosting I am working with requires the myname_ to be appended to t...

Convert mysql php join to PDO join

I have a join using mysql in php and want to turn that into a pdo query how do i do that? Also how do I get the results of this query and display it. Code is below: $query = "SELECT * FROM pages LEFT JOIN templates ON pages.template_id = templates.template_id WHERE pages.page_Title = '".$getVars['page']."'"; I am new to PDO so thi...

How to insert data in PDo

I use PDO and i can't insert some data: use this code: $sql = 'INSERT INTO `releases` (id, artists, release, label, catalog, date, tracklist, type, status, vote, votes_count) '; $sql .= 'VALUES (:id, :artists, :release, :label, :catalog, :date, :tracklist, :type, :status, :vote, :votes_count)'; $query = $this->db->prepare($sql); $qu...

PHP PDO using MySQL driver doesn't work - MAy be my code?

Okay I'm trying to convert MySQL request to PDO MySQL. I already do that before but I cn't get this one working : $query = 'SELECT key FROM turl WHERE key = "'.$k.'"'; $req = $db->query($query); if($req->rowCount() > 0) { $key = $row['key']; } And here are PDO php extentions : extension=php_pdo.dll ;extension...

getting the property names of stdClass to be listed on a page.

I have the following query: SELECT t1.extension NULLIF(t2.msetupfee,0) AS anual, NULLIF(t2.qsetupfee,0) AS bienal, NULLIF(t2.ssetupfee,0) AS trienal, NULLIF(t2.asetupfee,0) AS quadrienal, NULLIF(t2.bsetupfee,0) AS quinquenal FROM `tbldomainpricing` AS t1 INNER JOIN `tblpricing` AS t2 ON t1.id = t2.relid WHERE t2.type = 'domainre...

Postgres/PHP PDO::PDOStatement->bindParam() to character(1) field

I have code similar to the following: $data['someField'] = (isset($_POST['someField'])) ? 'Y' : 'N'; $stmt = $db->prepare("INSERT INTO public.someTable (someField) VALUES (':someField');"); $stmt->bindParam(':someField', ($data['someField']), PDO::PARAM_STR, 1); $db->beginTransaction(); $stmt->execute(); $db->commit(); The field is a ...

PHP PDO fetch results

I have the following script which runs fine but I think a) it is ugly; b) it doesn't do what I need after the script is run. Utlimately, the script should work as follows: 1) SQL selects all agents from a given table where the enrollment date is within the last month, grouped together by agent number where the count is greater than ...

SQLite, PDO, and PHP: New inserts not being saved

I have a few PHP (version 5.1.6) scripts and an sqlite3 database. For a while, everything was working fine, including inserts, updates, selects, and deletes. Now: Select statements still work fine Inserts, deletes, and updates all fail without an error that I can see. A -journal file is created in the same directory as the database, ...

Export sqlite table using php

Hi, Im just getting started with PHP, and I need to export a table from my sqlite database to a CSV or ideally XLS. I have found an example using mysql, but i cant convert it to work with sqlite. Here is what i have so far: <?php $db = new PDO('sqlite:../ccm.sqlite'); $query = $db->query('SELECT * FROM Emails'); $export = sqlite...

2nd PDO exec get MySQL Syntax error or access violation: 1064

Hello! I am new here. The code and error below I try to solve in three days. Could anyone help me? $db = new PDO('mysql:dbname=' . DBNAME . ';host=' . DBHOST, DBUSER, DBPASS, array(PDO::ATTR_PERSISTENT => true)); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->exec("INSERT INTO A (phone, city, address) VALUES ('12345...

Form Variable in PDO Query

A Submit button for a form located on my page triggers the code below, but I am unsure of how to save the numeric value of a textbox named 'amount' into a php variable which I can use in the below PDO query. I am unsure of the syntax. Thanks in advance! if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) { $amo...