1

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?

6
  • 1
    require_once 'medoo.min.php'; require_once('signin.php'); Spot the difference Commented May 18, 2014 at 20:02
  • 1
    You said your code is from the signin.php file, but the error you pasted says it's in example.php. If you didn't change the error text, what is on line 4 of example.php? Commented May 18, 2014 at 20:04
  • BTW, you can use require_once with or without the parentheses. Commented May 18, 2014 at 20:06
  • 1
    You say the class but I don't see any classes anywhere here Commented May 18, 2014 at 20:11
  • @myesain I obfuscated the file system directory, it is in fact pointing to the correct file. Commented May 18, 2014 at 20:53

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

This line:

require_once('signin.php');

is inside a class but outside a method, which is not possible in PHP.

1 Comment

Just in case the above is not clear - require_once CAN be used inside methods/functions/constructors. You cannot use require_once to include the methods/functions themselves
1

I might be wrong, but doesn't require_once 'medoo.min.php'; require parentheses? like so: require_once ('medoo.min.php');

1 Comment

No, and it's considered bad practice in general to use parentheses for language constructs like require_once, even though it will work.
0

Try this, its likely to work

<?
    require_once 'medoo.min.php';
    use Medoo\Medoo; //If this line raises errors, remove it

    // Initialize DB
    $database = new Medoo(
    // required
    'database_type' => 'mysql',
    'database_name' => 'name',
    'server' => 'localhost',
    'username' => 'your_username',
    'password' => 'your_password',
    );

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

One other bug I noticed is that you instantiated a the Medoo object (in other words, you called the Medoo class) using a Lowercase M of which shouldn't be the case. In addition remember to pass in the correct Database & Server values.

See this for more info if you're still facing challenges on that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.