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('bobby@gmail.com', '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?

muffinjello
  • 158
  • 1
  • 5
  • 16

4 Answers4

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.

ata
  • 3,398
  • 5
  • 20
  • 31
Patrick
  • 3,289
  • 2
  • 18
  • 31
2

This line:

require_once('signin.php');

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

Jonast92
  • 4,964
  • 1
  • 18
  • 32
  • 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 – MarcoZen Mar 25 '18 at 15:24
1

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

syntrax
  • 21
  • 7
  • 1
    No, and it's considered bad practice in general to use parentheses for language constructs like `require_once`, even though it will work. – FtDRbwLXw6 May 18 '14 at 20:09
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.