2

I am executing some queries and am getting the following the error:

Parse error: syntax error, unexpected '$section2' (T_VARIABLE) on line 22

Line 22 is:

$section2 = $db->prepare("INSERT INTO learning_style_scores VALUES (5,12,4)");

I don't have a clue why I am getting this, I have checked my syntax and all seems to be correct. It basically doesn't like anything after the $section1 query is executed

EDIT:

I understand this is prone to SQL injection but I am doing it like this for testing purposes only.

<?php
    session_start();

    try {
    $db = new PDO("mysql:dbname=questionnaire;host=localhost", "root", ""); 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    }


    catch(Exception $e)
    {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
    }


    $session = md5(session_id());

    // insert section1 data into database
    $section1 = $db->prepare('INSERT INTO section1 VALUES (7,"test")');

    $section1->execute();​


    // insert learning style score into database
    $section2 = $db->prepare("INSERT INTO learning_style_scores VALUES (5,12,4)");

    $section2->execute();​
    ?>
4
  • 1. Do you show us here your full and real code ? 2. Is this the correct file? 3. Please show us your full and exact error message which you get
    – Rizier123
    Commented Mar 9, 2015 at 22:36
  • @Rizier123 I can reproduce a syntax error locally, exactly as stated.
    – Scuzzy
    Commented Mar 9, 2015 at 22:37
  • 1
    You've nothing TO prepare. So use $db->query Commented Mar 9, 2015 at 22:42
  • @Rizier123 This is all my code, there is nothing else. Apart from HTML markup which has no effect on it Commented Mar 9, 2015 at 22:42

1 Answer 1

9

Your code has some weird characters after the semicolon of this line:

$section1->execute();​
$section2->execute();​  //same for this line

If you look into a hex editor you see this:

24 73 65 63 74 69 6f 6e 31 2d 3e 65 78 65 63 75 74 65 28 29 3b e2 80 8b  
                                                             //^^^^^^^^This bit right here

//And it should look like this:
24 73 65 63 74 69 6f 6e 31 2d 3e 65 78 65 63 75 74 65 28 29 3b  

See here:

enter image description here

(Yeah I know my circles aren't the nicest)

And this is how it should look like:

enter image description here


Solution?

Just write the statement new with your keyboard and your fingers.

9
  • 1
    Ha, Confirmed, Just hit delete/backspace after the ; and the code parses.
    – Scuzzy
    Commented Mar 9, 2015 at 22:44
  • @Scuzzy Yes here this works too! You should notice that if you hit delete after the semicolon that it don't deletes the semicolon, it deletes the weird character
    – Rizier123
    Commented Mar 9, 2015 at 22:51
  • 1
    Alternative solution: applying code formatting (CTRL+SHIFT+F) in Eclipse (presumably other editors as well) automagically gets rid of the extra chars making the code parseable.
    – lafor
    Commented Mar 9, 2015 at 22:51
  • Yes, this was it. I would probably never have figured that out myself, probably the most bizzare issues I've had in my whole time programming. Thank you! Commented Mar 9, 2015 at 23:01
  • 1
    @user3574492 You're welcome :D! most bizzare issues I've had in my whole time programming until now! Maybe the future will bring a better one.
    – Rizier123
    Commented Mar 9, 2015 at 23:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.