Why I love PhpStorm: SQL GROUP BY handling

2 Replies

Here’s another post from my series why I love JetBrains PhpStorm. This is a SQL query, where I have I list of column names. The IDE not only marks the columns missing in the GROUP BY clause as errors, it also offers an option to fix it for you by adding them to the GROUP BY clause.

PhpStorm understands SQL mixed with PHP code.

More PhpStorm love:
SQL table aliases
JavaScript code completion with dojo

Streaming large data (LOBs) directly into an Oracle database with PHP


The OCI library allows you to stream large data in chunks directly into a LOB instead of loading it completely into memory first. This technique is only mentioned in the article Working with LOBs in Oracle and PHP on the Oracle Technology Network, but no code example is given there, so here’s my take:

$sql = "INSERT INTO myTable (myUploadBinary)
  RETURNING myUploadBinary INTO :myBinary";
$stmt = oci_parse($conn, $sql);
$lob = oci_new_descriptor($conn, OCI_D_LOB);
oci_bind_by_name($stmt, ":myBinary", $lob, -1, OCI_B_BLOB);

$handle = fopen("myFile", 'r');
while (($buffer = fread($handle, 2048)) != '') {

PHP Tip: Binding variables in a SQL WHERE IN clause

3 Replies

Have you ever wondered if it is possible to bind an unknown amount of variables in a SQL WHERE IN clause, e.g.:

SELECT id, text FROM myTable WHERE id IN (:myId1, :myId2, myId3, ...)

Use a OCI collection object and wonder no more:

$keyList = array(100, 250, 350);
$stmt = oci_parse($cnn, "SELECT id, text FROM myTable WHERE id IN (SELECT column_value from table(:myIds))");
$coll = oci_new_collection($cnn, 'ODCIVARCHAR2LIST','SYS');
foreach ($keyList as $key) {
oci_bind_by_name($stmt, ':myIds', $coll, -1, OCI_B_NTY);

while($row = oci_fetch_array($stmt, OCI_ASSOC)) {
    echo "{$row['ID']}, {$row['TEXT']}";

I found the above solution by ThinkJet on stackoverflow.

SQL tip: How to bind NULL with PHP and Oracle


Neat little trick to bind a null value without using an if clause.

How often have you done something like this when a variable might be null:

$sql = "SELECT * FROM someTable WHERE col1 = :firstVar";
if (isset($secondVar)) {
   $sql.= " AND col2 = :secondVar";
oci_bind_by_name($stmt, ':firstVar', $firstVar);
if (isset($secondVar)) {
   oci_bind_by_name($stmt, ':secondVar', $secondVar);

Then checkout this, which is quite a bit shorter and a lot easier to read:

$sql = "SELECT * FROM someTable WHERE col1 = :firstVar
   AND (col2 = :secondVar OR :secondVar IS NULL)";
oci_bind_by_name($stmt, ':firstVar', $firstVar);
oci_bind_by_name($stmt, ':secondVar', $secondVar);