Skip to main content

Posts

Showing posts with the label php

Quickly cast array elements in PHP

It's not immediately obvious, but there is a concise way to cast all elements of an array to a single type.  It's all possible with the array_walk() function. array_walk($test, function(&$value, $key){$value = (integer) $value;}); Used in an example it would look like this. $sampleArray = array("one"=>"1", "two"=>"2", "three"=>3, "four"=>"4d"); var_dump($sampleArray); array_walk($sampleArray, function(&$value, $key){$value = (integer) $value;}); //--------------------------------^ Required! ^ //--------------------------------------------------------+ Type to cast to. var_dump($sampleArray); NOTE:  It's important to note that the ampersand is necessary.  See the note about references on the second argument of the array_walk() function documentation.

Consolidated query executions version 2

This is an extension of a piece from late last year titled, " Consolidated query executions ."  That version had a problem in that it didn't work as advertised with MySQL .  As a result it has been re-worked and listed here.  It's pending further testing, specifically against SQL Server , it's just listed now in the event that it may prove useful to someone. private function run($sql) { $Query = $this->Conn->prepare($sql); if((func_num_args() == 2) && is_array(func_get_arg(1))) { $Query->execute(func_get_arg(1)); } else { $Query->execute(array()); } $errorInfo = $Query->errorInfo(); if($errorInfo[1] != NULL) { return $errorInfo[2]; } $rowCount = $Query->rowCount(); $rowCountStatus = $Query->errorCode(); $fetch = $Query->fetchAll(\PDO::FETCH_ASSOC); $fetchStatus = $Query->errorCode(); if(($rowCountStatus == "00000") && ($fetchStatus == "00000")) { return $fetch; } els...

Eclipse KB: PDT fails to install with Juno

Issue Eclipse PDT 3.1.x fails to install on Eclipse Juno for Ubuntu 12.10. Cause Unknown, but the errors reported indicate a broken path of some sort.  It may be a broken path in a URL the package manager is using.  Or it may be a broken path in the package itself. Solution Point Eclipse to the PDT-specific repository. http://download.eclipse.org/tools/pdt/updates/release Environment Eclipse version 4.2 Juno Ubuntu PDT version 3.1.x Ubuntu version 12.10 Details The install software feature of Eclipse has a predefined repository for Juno with the following URL. http://download.eclipse.org/releases/juno This repository has an option for the PHP Development Tools (PDT) package under the following path. Programming Languages | PHP Development Tools (PDT) SDK Feature Installing PDT from this repository fails.

Consolidate query executions

UPDATED:  There is an issue whereby this code does not work as expected with MySQL.  As a result, the concept has been reworked and listed under the entry " Consolidated query executions version 2 ." In an effort to adhere to the DRY principle, or don't repeat yourself , the routine defined in this piece can be added to programs that employ a series of queries.  The idea is that it moves a few lines of code that would otherwise need to be in each method or function that calls a query to a database server. This snippet is coded with PHP and represents a method from a larger class.  There are more details about the code itself in the comments of the code. private function run($sql) { // Assumes $this->Conn has been set up in advance as a PDO connection. $Query = $this->Conn->prepare($sql); if((func_num_args() == 2) && is_array(func_get_arg(1))) { $Query->execute(func_get_arg(1)); } else { $Query->execute(array()); } // ...

PHP KB: No records found when using SQL variables and PDO parameters

Issue The PDO class for PHP throws an error when running queries against SQL Server and returning multiple results sets. Cause Without the benefit of the details around the implementation of SQL Server or the SQLSRV PDO driver, there's no way to know what's causing this issue.  However, the anecdotal evidence points to the the lack of support in the SQLSRV driver for SQL Server's ability to return multiple datasets. One of the errors that has been encountered reported a question mark (?) placeholder in each place where an SQL variable was in the script that was passed to the PDO::prepare() method call.  This was taken to mean that the method was perceiving each SQL variable as a parameter that it should be looking for in the parameters array passed in the PDOStatement::execute() method.  In other words, it was interpreting SQL parameters (variables) as PDO parameters (?). Ultimately, this issue is likely to come down to the use of the table data type or tempo...