0

I looked at many other questions, but I can't find my own answer in it. here is my syntax error (unexpeted T_IF):

while(($rij1 = mysql_fetch_object($result1))
and( if ($voornaam=NULL) {
            $rij2 = ' ';}
elseif($voornaam!=NULL){
            $rij2 = mysql_fetch_object($result2);})

I looked at the line before the syntax, but I couldn't find what is wrong... Does someone know it?

hakre
  • 193,403
  • 52
  • 435
  • 836
user1813397
  • 41
  • 1
  • 1
  • 2
  • 1
    What's with the `and` and `if` there? `and` expects expressions as its operands, not statements. What are you trying to do? – Felix Kling Nov 09 '12 at 21:08
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained and the [deprecation process](http://j.mp/Rj2iVR) has begun on it. See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – tereško Nov 09 '12 at 21:24

1 Answers1

4

Try rewriting your code as:

while ($rij1 = mysql_fetch_object($result1))
{
    if ($voornaam === NULL) 
    {
        $rij2 = ' ';
    } 
    else
    {
        $rij2 = mysql_fetch_object($result2);
    }
}

Edit: Corrected your condition in the first if, as @andrewsi spotted - = is an assignment operator, so previously your code was changing $voornaam to NULL, then checking if the result evaluated to true (which, of course, it never would - so the second block would always execute)

In your original code, you're using the and operator - presumably having seen it used in some well meaning but poorly coded examples like mysql_connect(...) or die('an error occurred');.

What's happening in that example is that the result of the first statement - mysql_connect() - is checked. If it evaluates to true, the second statement never executes, but if it evaluates to false then the second statement - die('an error occurred'); - is executed. As you've just discovered, this pattern is confusing and best avoided.

Kelvin
  • 5,227
  • 1
  • 23
  • 36
  • 2
    The if statement should be `if ($voornaam==NULL) {` – andrewsi Nov 09 '12 at 21:07
  • +1 Proper indenting makes code so much more readable – Arjan Nov 09 '12 at 21:09
  • thank you all, haha sorry I don't do this often... But now I got a syntax error on line 22, an unexpected '{' (line 22 is the first line here) – user1813397 Nov 09 '12 at 21:12
  • @user1813397 Your code also had an extraneous `(` in the `while` statement, which caused that error - I've removed it in my latest edit. – Kelvin Nov 09 '12 at 21:16
  • okay thank you all! It works all fine now! – user1813397 Nov 09 '12 at 21:17
  • 1
    @KelvinMackay: The else-if was sense-less (always true), also compare with NULL with `===` three. If you keep some little space in there, it's more clear. Just for the code here, you don't need to write code that way at home. – hakre Nov 09 '12 at 21:17
  • @hakre Thanks, you're right that scans much better. I'll keep that in mind in future :) – Kelvin Nov 09 '12 at 21:19