PHP generating a phantom warning for code that is not executed

In an effort to tidy up my code, I've encapsulated my database reads into its own function, which accepts a SQL command for the argument, as well as an optional $getArray argument which specifies whether I am returning an array as the result (true for SELECT, false for UPDATE, etc).

function readDB($sql, $getArray = false)
    $conn = odbc_connect('myAccessDataBase', '', '');
    if (!$conn)
        return exit('Connection Failed: '.$conn);
    if ($getArray = false)
        odbc_exec($conn, $sql);
        $res = odbc_exec($conn, $sql);
        if (!$res)
            return exit('Error in SQL');
        return odbc_fetch_array($res);

The code above works as expected except for one small caveat. If I am performing an UPDATE vs a SELECT, I get a warning due to $getArray being set to false:

Warning: odbc_fetch_array() [function.odbc-fetch-array]: No tuples available at this result index in C:...\functions.php on line 14

I understand the warning, which makes sense if that line were to actually be executed, however it is not. My question is in regards as to why PHP is evaluating the contents of the else portion of my if statement, which is not even being hit during runtime. I understand that the UPDATE cannot return an array, hence why I am evaluating $getArray to determine whether I expect such a return. What can I do to circumvent this warning? Regardless, I am still pretty fresh with my PHP, and am probably going about this the wrong way anyhow. Cheers!


You're setting the variable TO FALSE instead of evaluating it FOR FALSE If you change this line:

if ($getArray = false)


if ($getArray == false)

It should work.


On another note, you should do your test in reverse. Instead of testing for false, test for true and THEN have an else. It makes a little more sense logically for anyone coming in to look at your code later.

Instead of If this IS NOT the case then do this it makes more sense to say if this is the case, do this

So switch your code blocks around and test:

if ($getArray)

Need Your Help

Authenticate user after password reset in Django

django django-authentication

I use standard Django view, password_reset_confirm(), to reset user's password. After user follows password reset link in the letter, he enters new password and then view redirects him to the site ...

Is Django 1.2.3 competable with Python 2.6.5

python django

Just curious to know, Test module won't work properly with django 1.2.3 when update from django 1.1.1 (now on python 2.6.5)

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.