简体   繁体   中英

Establishing database connection through php class

I am currently learning and working with php classes. I have created a basic class in hopes to connect with mysql database and fetch results from two tables. I am using PDO style. In a separate file called db_con.php

I have my credentials to connect with my db. I am having difficulties in establishing and fetching the values successfully. I am not able to use $db_con from the db file inside the SelectList class to establish a connection.

I am getting an error in this line in particular:

$sql = $this->db_con->prepare("SELECT * FROM curriculum_selection_list");

The error states:

Fatal error: Call to a member function prepare() on a non-object in /data/24/2/26/14/2678177/user/2940861/htdocs/controlpanel/select.class.php

How can I fix this?

select.class.php

include "db_con.php";

class SelectList
{
    private $db_con;

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

        public function ShowCurriculum()
        {
            $sql = $this->db_con->prepare("SELECT * FROM curriculum_selection_list");
            $sql->execute();
            $data = $sql->fetchAll();
            $curriculum = '<option value="0">choose...</option>';
            foreach ($data as $row){
                 $curriculum .= '<option value="' . $row['curriculum_id'] . ':' . $row['curriculum_name'] . '">' . $row['curriculum_name'] . '</option>';
            }
            return $curriculum;

        }

        public function ShowCourse()
        {
            $sql = $this->db_con->prepare("SELECT * FROM course_selection_list");
            $sql->execute();
            $data = $sql->fetchAll();
            $course = '<option value="0">choose...</option>';
            foreach ($data as $row){
                 $course .= '<option value="' . $row['course_id'] . ':' . $row['course_name'] . '">' . $row['course_name'] . '</option>';
            }
            return $course;
        }
}

$opt = new SelectList();

db_con.php

$dsn = '';
$user = '';
$password = '';

try {
    $db_con = new PDO($dsn, $user, $password);
    $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
} 

you need a global declaration to access a variable assigned outside the function:

public function __construct()
{
    global $db_con;
    $this->db_con = $db_con;
}

But it would be better to either pass the connection in as a parameter when creating the object, or define a function in db_con.php and call it from the constructor.

As mentioned you are getting error into below line:
$sql = $this->db_con->prepare("SELECT * FROM curriculum_selection_list");
In this above code it is not able to create connection with db.php
please try
class SelectList
{
global $db_con;
....
....
.....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM