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
869 views
in Technique[技术] by (71.8m points)

php include - Php Error - Unexpected require_once expecting function

I'm currently trying to fetch the medoo framework so that I can begin easily retrieving data from my MySQL database... and for some reason it doesn't work!

here's the code of my signin.php file

<?php
    function loginUserAccount($loginname, $password){
    // Include Medoo (configured)
    require_once 'medoo.min.php';

    // Initialize
    $database = new medoo();

    $email = $database->get('MM_Users', 'Email', [
        'ID' => 1
    ]);
    return $email;
    }
    ?>

And the error:

Parse error: syntax error, unexpected 'require_once' (T_REQUIRE_ONCE), expecting function (T_FUNCTION) in /myfiledirectory/example.php on line 4

Any help is greatly appreciated! I'm not quite sure at what's going wrong.

The php file that's running is :

<?php
require_once('signin.php');
if(!loginUserAccount('[email protected]', 'AveryRandomPassword')){

    echo 'error';
} else {
    echo $email;
}
?>

There's also a difference in the wrapping for require_once... this doesn't make a difference does it? And if so, which one is more advisable to use in a production environment?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You can't have require_once inside a class without a function. That's the main reason.

Try putting the require_once in the construct.

To be exact :

class foo 
{
    require_once('bar.php');
}

will throw an error.


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

...