Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - Parse error: syntax error, unexpected (T_VARIABLE)

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(), "
";
    }


    $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();?
    ?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...