pdo

PDO::fetchAll vs. PDO::fetch in a loop

Just a quick question. Is there any performance difference between using PDO::fetchAll() and PDO::fetch() in a loop (for large result sets)? I'm fetching into objects of a user-defined class, if that makes any difference. My initial uneducated assumption was that fetchAll might be faster because PDO can perform multiple operations in ...

Getting the record ID just added with mysql prepared statements

I'm inserting a record using PDO and saving the result in $result which I use as a boolean $result = $addRecord->execute(); if ($result){ //add successful } else { //add unsuccessful } I'd like to also get the record id just added. In the table, each record has an auto_incremented field called id. I tried doing this $new_id =...

Syntax for "RETURNING" clause in Mysql PDO

I'm trying to add a record, and at the same time return the id of that record added. I read it's possible to do it with a RETURNING clause. $stmt->prepare("INSERT INTO tablename (field1, field2) VALUES (:value1, :value2) RETURNING id"); but the insertion fails when I add RETUR...

DB Cursor fetching in Web Applications

I never understood this: in web development whe[n|re] does it make sense to fetch results one by one? I mean, why should I use PDOStatement->fetch*() when I can use PDOStatement->fetchAll()? ...

Can PHP's PDO be limited to a single query?

PHP's PDO allows multiple querys to be executed at once, either via the query() method or as a prepared statement. Both of the following examples work: // Two SQL queries $query = "SELECT * FROM table; DROP table;" // Execute via query() $pdo->query($query); // Execute via prepared statement $stmt = $pdo->prepare($query); $stmt->execu...

Using PHP and SQLite

I am going to use a small SQLite database to store some data that my application will use. I cant however get the syntax for inserting data into the DB using PHP to correctly work, below is the code that i am trying to run: <?php $day = $_POST["form_Day"]; $hour = $_POST["form_Hour"]; $minute = $_POST["form_Minute"]; $t...

PHP PDO MySQL IN (?,?,?...

I want to write a MySQL statement like: SELECT * FROM someTable WHERE someId IN (value1, value2, value3, ...) The trick here is that I do not know ahead of time how many values there will be in the IN(). Obviously I know I can generate the query on the go with string manipulations, however since this will run in a loop, I was wonderi...

Counting number of rows in MySQL table using PDO

Hi everyone, Need to count the number of rows for each category in a MySQL table using PDO. For example, I need to have the number of entries for category 1, category 2, etc. If possible, I would like to do this without having to write out an SQL statement for each category. Thanks! ...

Is SQL used by PDO database independent?

Different databases have slight variations in their implementations of SQL. Does PDO handle this? If I write an SQL query that I use with PDO to access a MySQL database, and later tell PDO to start using a different type of database, will the query stop working? Or will PDO 'convert' the query so that it continues to work? If PDO does ...

Questions about shifting from mysql to PDO

Hey guys I have recently decided to switch all my current plain mysql queries performed with php mysql_query to PDO style queries to improve performance, portability and security. I just have some quick questions for any experts in this database interaction tool Will it prevent injection if all statements are prepared? (I noticed on ph...

how to have defined connection within function for pdo communication with DB

hey guys I just started trying to convert my query structure to PDO and I have come across a weird problem. When I call a pdo query connection within a function and the connection is included outside the function, the connection becomes undefined. Anyone know what I am doing wrong here? I was just playing with it, my example is below. i...

PHP and use of the Num_Of_Rows() function?

Below is some PHP code that i have written, the problem occurs when it gets to the use of the num_of_rows(), it just does not seem to work and i cant figure out why? <?php try { $divMon_ID = array(); $divMon_Position = array(); $divMon_Width = array(); $divMon_Div = array(); $db = new PDO('sqlite:db/EVENTS.sqlite');...

singleton factory connection pdo

Hey guys I am having a lot of trouble trying to understand this and I was just wondering if someone could help me with some questions. I found some code that is supposed to create a connection with pdo. The problem I was having was having my connection defined within functions. Someone suggested globals but then pointed to a 'better' sol...

PDO not working within function

Hi everyone, I am trying to make a function to pull a page's content from a MySQL table using a PDO Prepare statement. My code works just fine outside of the function I defined, but no matter what I do it will not work within the function - I receive the following error: Fatal error: Call to a member function prepare() on a non-object ...

PDOException “could not find driver”

I have just installed Debian Lenny with Apache, MySQL, and PHP and I am receiving a PDOException could not find driver. This is the specific line of code it is referring to: $dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS) DB_HOST, DB_NAME, DB_USER, and DB_PASS are constants that I have defined. It work...

PHP - OCI and PDO OCI failed to run

Hi there, I am trying to install lighttpd and php separately on Windows XP. I successfully get the server to run. However, I failed to get the OCI to run in my server. Error message saying that failed to load the PDO Driver for OCI. When I check on phpinfo(), no OCI/PDO OCI driver is loaded. I got the correct dll file and php.ini corr...

PHP PDO fetch null

How do you check if a columns value is null? Example code: $db = DBCxn::getCxn(); $sql = "SELECT exercise_id, author_id, submission, result, submission_time, total_rating_votes, total_rating_values FROM submissions LEFT OUTER JOIN submission_ratings ON submissions.exercise_id=submission_ratings.exercise_id WHERE id=:id"; $st = $db->p...

PHP's PDO ODBC connection is failing to connect to Intersystems Caché DB

I have the developer version of the Caché DB running locally. When I try to use PDO ODBC to connect to it I just get errors: Connection failed: SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified I've used several DSN statements such as: #$conn = "odbc:host=127.0.0...

PHP PDO: Fetching data as objects - properties assigned BEFORE __construct is called. Is this correct?

The full question should be "Is this correct or is it some bug I can't count on?" WHY is this correct behavior? I've been working with PDO more and in particular playing with fetching data directly into objects. While doing so I discovered this: If I fetch data directly into an object like this: $STH = $DBH->prepare('SELECT first...

Escape arguments for PDO statements?

New to PDO - do I need to escape arguments I'm passing into a PDO prepared statement (such as the following): $_GET['name'] = "O'Brady"; $sth = $dbh->prepare("INSERT INTO users SET name = :name"); $sth->bindParam(':name', $_GET['name']); $sth->execute(); ...