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>