RSS

  • YouTube
  • LinkedIn
  • Google

Archives for : PHP

Binding several values in PDO SQL Statement (PDOStatement)

Sometimes we need to search a single value through several columns when building SQL instructions.

Suppose the table below

  • [ENTITY_ONE]
    • ID
    • COLUMN_A
    • COLUMN_B
    • COLUMN_C
    • COLUMN_D
    • COLUMN_E

If we need search a single value on columns B,D and E we will need use the following instruction

In PHP code we can do

Well, this can work but we know that isn’t the best approach. We need use Binding Values to avoid SQL injection and other malicious treats.

So, the code can be modified to

Much better, but, when building complex SQL instruction, things can be confusing with lots of arguments and don’t forget: ORDER MATTERS.

Happily PDO can bind values in different order when using named bindings.

Hmm, seems that this isn’t good enough. We only change the use of 1-indexed placeholder to a :named placeholder. There’s no gain beyond of code readable and the possibility to bind in any order.

Yes, but now we can do the best approach when using one unique search term in several columns. We can use only one bind to one or more :named placeholders ’cause PDO is smart and clever. Look our final code here.

Can save a lot of typing when writing many SQL instruction using same argument.

Naming files using list from 0 to Z

Today I was coding some scripts and found a little trouble to use a defined pattern.

The pattern is to create files where the sequence starts in 0 (zero) and cannot be repeated until Z.

Example:

myfile0.ext, myfile1.ext, myfile2.ext, (...), myfile9.ext, myfileA.ext, myfileB.txt, (...), myfileZ.txt

Well, this is not a big trouble so I did use this code.

But $seq did not gave the expected value of 0 (zero) on first run. Instead, it was blank.

Debugging the variables, I saw that the while never evaluates to true. Attempting to reproduce on command line I saw that in_array($seq, $seqs); always return true. I tryed to use “”, “R” and no matter what value I used, still returning true.

So I change to use STRICT argument for in_array to true and works for ‘A’ through ‘Z’, but not for ‘0’ through ‘9’.

Damn… PHP is right, “0” is not strictly equals to 0. The Chr function return string and range('0', '9') creates an array with integer values.

So, I changed the approach to evaluate all values with STRICT, because I would like to create a nice and clean code without no other functions to be used.

This is the final code that I’m using:

How you can see, I changed the $seqs initial values from ‘0’ to your ASCII code and get back to your value that gave me an array with all values in string type.

See you!

PHP Comparison Error

Today, I was writing a script in PHP to be used in the command line when I came across unexpected behavior (at least on my part).
The script should receive three arguments, the last of them a list containing one or more numeric codes.
Trying to validate this last argument was getting a different result than imagined.
See the code snippet that I was using:

Continue Reading >>

Converting xor, shr and shl from Delphi to PHP

I'm migrating a software made in Delphi to PHP and I came across a problem in a fucking Blessed encryption function.

I suffered a bit trying to rewrite the function does the complexity of the code and with the differences in the results.

The function makes use of operators XOR and SHR in Delphi version. The SHR was relatively easy thanks to experience with bitwise operators and how Delphi documentation says that the operator pulls off bits to the right

Continue Reading >>