Why is your PHP output still buffered even when you turned buffering off?

Reply

If you want to stream your content with PHP, you need to have output_buffering set to off in you php.ini and/or use it together with flush(). If this still does not work check your Antivirus software.

On Windows, the on-access scanners of Sophos and Avira Antivirus buffer your http output! The only solution is to either switch to https or turn on-access scan off.

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

Reply

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)
  VALUES (EMPTY_BLOB())
  RETURNING myUploadBinary INTO :myUpload";
$stmt = oci_parse($conn, $sql);
$lob = oci_new_descriptor($conn, OCI_D_LOB);
oci_bind_by_name($stmt, ":myUpload", $lob, -1, OCI_B_BLOB);
$this->execute($stmt);

$handle = fopen("php://input", 'r');
while (($buffer = fread($handle, 2048)) != '') {
  $lob->write($buffer);
  $lob->flush();
  flush();
  ob_flush();
}
fclose($handle);
oci_commit();
oci_free_statement($stmt);
$lob->free();

PhpStorm: Complete code versus complete current statement

Reply

Here are two useful keyboard shortcuts for PhpStorm which sound similar, but are quite different:

complete code CTRL + SPACE
will either finish your code if what you typed so far is unambiguous or otherwise offer a list to choose from.

complete current statement CTRL + SHIFT + ENTER will add a semicolon at the end of your statement, no matter where the cursor is.

If you want to train your shortcut skills for PhpStorm or other software, the website shortcutFoo is really helpful and fun to use.

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) {
   $coll->append($key);
}
oci_bind_by_name($stmt, ':myIds', $coll, -1, OCI_B_NTY);
oci_execute($stmt);

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

Reply

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);

REST with dojo and PHP: Notes on using dgrid with a caching store

18 Replies

I would like to share some (personal) pitfalls I came across when creating the demo REST with dojo and PHP: Demo of a dgrid observing changes to the JsonRest store:

  • dojo/store/Observable only works correctly when the object has an id. Unfortunately, dojo/store/Memory.add(object) does not add the id to the object when creating it (as of 1.7.1) .¬† See bugs #12835 and #14281. For a fix see changeset #27072
  • Observable does only work correctly with a store, that provides a queryEngine. dojo/store/JsonRest does not have a queryEngine by default, so you will have to provide your own. If you are using a cache, observe the dojo/store/Memory directly. Upate 29.06.2012: For a fix see changeset¬†#29069.
  • As stated in the docs, but easily missed: dojo/store/Cache automatically adds all returned objects from get() and query() calls to the cache, but only get() uses the cache, whereas query() directly uses the master store. If you want the query to come from the cache, directly call query() on the caching store, e.g. dojo/store/Memory.
  • In the demo, the PHP script responds to the GET 2 by returning a Json object with id 2 and to a GET 2/ by returning an array of objects with parent id 2. If you want to query the Memory store for 2’s children, you have to provide an object as an argument, e.g. storeMemory.query({parId: 2}), whereas the JsonRest store expects a string e.g. storeJsonRest.query(‘2/’);
  • JsonRest store’s methods do not return the same types as Memory store, which can be confusing…
    storeMemory.add(newObject) returns the id
    storeJsonRest.add(newObject) returns the response from the server (or rather a Deferred providing it)
    …, but makes sense, because it can save an extra round trip to the server.

Also see dojo/store/Observable and dojo/store/Cache more info.

 

Installing Xdebug on Windows and PhpStorm

Reply

There is a catch when installing Xdebug on windows: You need to set the full path of the extension directive in the php.ini, since the default directory is only used for non-zend extensions. Also don’t forget to use quotes.

;used only for 'normal' extensions
extension_dir = "C:Program Files (x86)PHPext"

;still necessary to use full path
zend_extension = "C:Program Files (x86)PHPextphp_xdebug.dll"

;won't work
;zend_extension = php_xdebug.dll

To make Xdebug work in PhpStorm you also need to enable remote debugging:

xdebug.remote_enable = 1

Why I love JetBrains phpStorm II

Reply

Here’s another example for the reason why I like phpStorm so much: SQL code assist in the Database Console completes statements either in upper case or lower case depending on how you started typing, not to speak of understanding aliased tables when suggesting column names after typing a dot.

Screenshot: SQL code assist in PhpStorm understands aliased tables.