Article View: comp.os.vms
Article #59Re: PHP "Features"
From: Arne Vajhøj
Date: Sat, 24 Oct 2026 15:59
Date: Sat, 24 Oct 2026 15:59
142 lines
4937 bytes
4937 bytes
On 10/24/2026 8:12 AM, Simon Clubley wrote:
> Some simple examples:
>
> 1) Equality in PHP is loose equality "==" by default. You have to use
> "===" to get strict equality. This leads to warnings like this scattered
> throughout the PHP manual: (See the Return Values section.)
>
> https://www.php.net/manual/en/function.fgetc.php
>
> This is a great source of subtle errors. I don't like to see PHP code
> that uses loose equality for this reason.
The type juggling thing is not good.
Sins of the past.
Always === is a good workaround.
> 2) Things that should be errors are warnings and execution continues by
> default unless you make the error reporting level more strict in your
> code. I always use strict error reporting when I have to use PHP for
> this reason.
Strict is good.
I believe you can change it globally in php.ini.
> 3) mysqli_real_escape_string() doesn't always work:
>
> https://www.php.net/manual/en/mysqli.real-escape-string.php
>
> (See the Warning: section)
It always works as documented and intended.
First note that the recommended way to use mysqli is to use prepare.
mysqli_real_escape_string only exist for extraordinary circumstances.
Regarding the warning then:
# This function does not add quotes around the result; they must be
# added by the caller.
Yes.
$ type simon.php
<?php
function test($s) {
$con = mysqli_connect('ARNEPC5', 'arne', 'hemmeligt', 'Test');
echo sprintf("%s -> %s\n", $s, mysqli_real_escape_string($con, $s));
}
test('AB');
test("A'B");
test('A\B');
?>
$ php simon.php
AB -> AB
A'B -> A\'B
A\B -> A\\B
But there is a reason that the function is called escape
and not escape_and_quote - it just escapes - it does not quote.
People need to use:
$sql = sprintf('SELECT ... WHERE ... f = '%s',
mysqli_real_escape_string($con, $f));
instead of just:
$sql = sprintf('SELECT ... WHERE ... f = %s,
mysqli_real_escape_string($con, $f));
(note that the old mysql_real_escape_string did
not quote either)
But that is what the function does.
# If the NO_BACKSLASH_ESCAPES SQL mode is enabled on the connection,
# only the single quote (') is escaped, so a value placed between double
# quotes (") can terminate the string and allow SQL injection.
Sounds a bit cryptic but is actually relative simple.
mysql> CREATE TABLE simon (
-> id INTEGER NOT NULL PRIMARY KEY,
-> txt VARCHAR(50)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO simon VALUES(1, 'A\\B');
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM simon;
+----+------+
| id | txt |
+----+------+
| 1 | A\B |
+----+------+
1 row in set (0.00 sec)
mysql> SET @@session.sql_mode = CONCAT(@@session.sql_mode,
',NO_BACKSLASH_ESCAPES');
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO simon VALUES(2, 'A\\B');
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM simon;
+----+------+
| id | txt |
+----+------+
| 1 | A\B |
| 2 | A\\B |
+----+------+
2 rows in set (0.00 sec)
PHP mysqli_real_escape_string put backslashes in front of problematic
characters.
MySQL NO_BACKSLASH_ESCAPES tell MySQL to not treat backslashes
special.
It is obvious that those two do not work well together.
Neither issue qualify as mysqli_real_escape_string not
working in my book.
I understand that they put the warning in though - I am sure
some users have messed up with these.
Arne
Technical Details
Message-ID:
<11bikkl$2rllh$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11bcbi1$k068$1@dont-email.me> <nk1gv8F1eecU1@mid.individual.net> <11bgih8$f7a$1@reader2.panix.com> <nk2bhaF5j2sU1@mid.individual.net> <11bh3ei$19g$1@reader2.panix.com> <11bi7bg$2mdi4$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Arne Vajhøj <arne@vajhoej.dk> Newsgroups: comp.os.vms Subject: Re: PHP "Features" Date: Sat, 24 Oct 2026 11:59:30 -0400 Organization: A noiseless patient Spider Lines: 142 Message-ID: <11bikkl$2rllh$1@dont-email.me> References: <11bcbi1$k068$1@dont-email.me> <nk1gv8F1eecU1@mid.individual.net> <11bgih8$f7a$1@reader2.panix.com> <nk2bhaF5j2sU1@mid.individual.net> <11bh3ei$19g$1@reader2.panix.com> <11bi7bg$2mdi4$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 24 Oct 2026 15:59:18 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3004081"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19tH+Rvx8z0sYe0GP63/6MYt+F0YLRzP80="; posting-host="90629bce1c2634862ed525d6a667363b" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:SM/dtfZlD95z1HmfJMGKKYjPYXo= sha256:xvzn1Onm9n8JqemWCKXsiLJb3gbDlXoQyRrOGoFhD74= sha1:JTTDxJfGlPCpFAbG0pP+RuKH2Iw= sha256:6gSCkpGF8UhFyjwVm1s90V9DyE4t/NG59WqkinSZHao= Content-Language: en-US In-Reply-To: <11bi7bg$2mdi4$1@dont-email.me>