0

I tried to divide the function to create a table and to connect to the database but the php function that creates the table does not work to me, does anyone know how to fix this problem? I enclose the php code and the index.

prova.php:

class db_mysqls
{
    public function connect()
    {
        $username="username";
        $password="password";
        try
        {
            $connection = new PDO("mysql:host=localhost;dbname=ijdb",$username, $password);
            $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo ' <br> Connection Complete';
        }
        catch (PDOException $e)
        {
            echo 'Connection failed: ' . $e->getMessage();
        }
    }

    public function createDB()
    {
        try
        {
            $sql="CREATE TABLE joke (
                    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
                    joketext TEXT,
                    jokedate DATE NOT NULL)";
            // use exec() because no results are returned
            $connection->exec($sql);
            echo "Table MyGuests created successfully";
        }
        catch (PDOException $e)
        {
            echo 'Creation failed: ' . $e->getMessage();
        }
    }
}
?>

index.php:

<html>
    <head>
        <title>Index.html</title>
    </head>
    <body>
        Eseguo prova.php <br>
        <?php
            include 'prova.php';
                $db = new db_mysqls();
                echo 'New Database Object Created';
                $db->connect();
                $db->createDB();
        ?>
    </body>
</html>
Riki AA
  • 21
  • 7
  • 1
    error reporting says what? http://php.net/manual/en/function.error-reporting.php and are there any other errors we should know about that you didn't include, if any? – Funk Forty Niner Jan 19 '17 at 15:12
  • deja vu ... exactly the same issue with scoping as one I was just looking at earlier, `$connection` is not a class member so your methods can't access it : http://stackoverflow.com/questions/41742851/unable-to-get-database-abstraction-layer-to-work#comment70681440_41742851 – CD001 Jan 19 '17 at 15:13
  • how can I se the error? I realized that doesn't work because it doesn't print the various echo command in the function of the page. – Riki AA Jan 19 '17 at 15:17

1 Answers1

2

The problem is variable scope. All your variables are local. You need to make a private class member to store the connection object so the same connection can be accessed by both methods.

<?php
class db_mysqls
{
    private $connection; 

    public function connect()
    {
        $username="username";
        $password="password";
        try
        {
            $this->connection = new PDO("mysql:host=localhost;dbname=ijdb",$username, $password);
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo ' <br> Connection Complete';
        }
        catch (PDOException $e)
        {
            echo 'Connection failed: ' . $e->getMessage();
        }
    }

    public function createDB()
    {
        try
        {
            $sql="CREATE TABLE joke (
                    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
                    joketext TEXT,
                    jokedate DATE NOT NULL)";
            // use exec() because no results are returned
            $this->connection->exec($sql);
            echo "Table MyGuests created successfully";
        }
        catch (PDOException $e)
        {
            echo 'Creation failed: ' . $e->getMessage();
        }
    }
}
?>
Luke
  • 418
  • 4
  • 11