|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-12-28 18:11 UTC] red at ixney dot net
Description:
------------
I always thought '||' and 'or' was the same.
Combined with a mysql_query() however, they are not.
These lines are the same:
mysql_query("$var") or die("help!");
mysql_query("$var") || die("help!");
where $var is a DROP, INSERT or CREATE query, but they are *not* the same if $var is a SELECT query; the returned value will not be a 'Resource ID' in the latter line.
Reproduce code:
---------------
mysql_query("DROP TABLE IF EXISTS foobar") || die('Cannot drop table');
mysql_query("CREATE TABLE IF NOT EXISTS foobar (foo INT NOT NULL AUTO_INCREMENT, bar TINYINT(1), PRIMARY KEY (foo))") || die('Cannot make table');
// Let's insert some random stuff
mysql_query("INSERT INTO foobar (bar) VALUES (0)") || die('Cannot add values 1');
mysql_query("INSERT INTO foobar (bar) VALUES (1)") || die('Cannot add values 2');
mysql_query("INSERT INTO foobar (bar) VALUES (0)") or die('Cannot add values 3');
mysql_query("INSERT INTO foobar (bar) VALUES (1)") or die('Cannot add values 4');
$handle1 = mysql_query("SELECT * FROM foobar WHERE 1") || die('Cannot select');
$handle2 = mysql_query("SELECT * FROM foobar WHERE 1") or die('Cannot select');
echo '$handle1 is '.$handle1.'<br><br>';
echo '$handle2 is '.$handle2.'<br><br>';
Expected result:
----------------
$handle1 is Resource id #2
$handle2 is Resource id #3
Actual result:
--------------
$handle1 is 1
$handle2 is Resource id #3
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 15:00:01 2025 UTC |
Nothing to do with Mysql, but more like a parser problem <?php $handle1 = fopen("test.php", "r") or die("fopen failed"); $handle2 = fopen("test.php", "r") || die("fopen failed"); var_dump($handle1, $handle2); // yields resource(stream) / bool(true) ?> Apparently || casts the resource to bool (true) and assigns it to the variable. IMHO the || should either produce a parse error in this context or behave like 'or'.