0

I'm trying to create a connection using function in my other php file. So I can access it in every php file. How can I call the function inside my class from another php file. What is the proper way creating instance of my function will I put it in the top of my file or inside my class? I try to run my system but the connection won't load.

connection.php

<?php

DEFINE ('host', 'localhost');
DEFINE ('username', 'root');
DEFINE ('password', '');
DEFINE ('database', 'librarysystem');

function getConnection()
{
$myDB = mysqli_connect(host,username,password,database);

if (!$myDB)
{
    trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error());
}
else
{
    return $myDB;
}
}                         
?>

Another file where I'm gonna call my function or method.

loginCRUD.php

<?php
error_reporting(0);

require_once ('../connection/connection.php');

$myConnection = getConnection();

class loginCRUD
{
public function readLogin($dbusername,$dbpassword)
{
    $statement = $myConnection->prepare("SELECT * FROM loginmodule WHERE loginUsername = ? AND loginPassword = ? LIMIT 1");
    $statement->bind_param("ss", $dbusername, $dbpassword);
    if($statement->execute())
    {
        $result = $statement->get_result();
        if($result->num_rows)
        {
            $row = $result->fetch_assoc();
            return $row;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}
}
(new loginCRUD)->readLogin($dbusername,$dbpassword);
?>
4
  • 1
    Shouldn't $myDatabase on the line where you define your statement just be $myConnection? Commented May 10, 2016 at 17:12
  • Pass $myConnection as an argument to a constructor for example. Commented May 10, 2016 at 17:13
  • error_reporting(0); why? Commented May 10, 2016 at 17:21
  • @gre_gor What the eye does not see, the developer does not have to fix hehehhe So annoying I agree Commented May 10, 2016 at 18:28

2 Answers 2

1

There can be at least two possible ways of passing $myConnection to your loginCRUD class.

First is to create $myConnection in a class itself:

class loginCRUD
{
    protected $dbCon;

    public function __construct() 
    {
       $this->dbCon = getConnection();
    }

    public function readLogin($dbusername,$dbpassword)
    {
        $statement = $this->dbCon->prepare("YOUR SQL")
        // other codes
    }
}

(new loginCRUD)->readLogin($dbusername,$dbpassword);

Second is to pass $myConnection as an argument to your class constructor:

class loginCRUD
{
    protected $dbCon;    

    public function __construct($myCon) 
    {
       $this->dbCon = $myCon;
    }

    public function readLogin($dbusername,$dbpassword)
    {
        $statement = $this->dbCon->prepare("YOUR SQL")
        // other codes

    }
}

$myConnection = getConnection();
(new loginCRUD($myConnection))->readLogin($dbusername,$dbpassword);
Sign up to request clarification or add additional context in comments.

1 Comment

I prefer your first example! So glad you help :)
0

The reason you are getting an error is due to your variable scope. In your loginCRUDclass you try to access a variable called myConnection, it fails to find it because the scope of the function is only that function. It doesn't know that you mean the global declared variable. A correct way of fixing this should be:

...
public function readLogin($dbusername,$dbpassword)
{
    global $myConnection;
    $statement = $myConnection->prepare("SELECT * FROM loginmodule WHERE loginUsername = ? AND loginPassword = ? LIMIT 1");
    $statement->bind_param("ss", $dbusername, $dbpassword);
    if($statement->execute())
    {
...

For reference: Accessing variables and methods outside of class definitions

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.