Here’s the code:
print_table.php:
----------------
<?php
function print_table($db)
{
foreach ($db->query("SELECT * FROM name_of_db") as $row)
{
echo $row['title'];
}
}
?>
mysql_connect.php:
------------------
<?php
class mysql_connection
{
private $_dsn;
private $_user;
private $_pass;
private $_db;
public function __construct()
{
$this->_dsn = "mysql:dbname=nope;host=nope";
$this->_user = "nope";
$this->_pass = "nope";
$this->_db = null;
}
public function connect()
{
try
{
$this->_db = new PDO($this->_dsn, $this->_user, $this->_pass);
}
catch (PDOException $e)
{
echo "Connection Failed: " . $e->getMessage();
}
}
public function get_db()
{
return $this->_db;
}
}
?>
init.php:
---------
<?php
include_once("mysql_connect.php");
include_once("print_table.php");
$con = new mysql_connection();
$con->connect();
?>
init.php is called at the very beginning of index.php. Then, later in index.php I have this line:
<?php print_table($con->get_db()); ?>
Why could that be?
Thank you!
I fixed it. The problem was that I was using SELECT * FROM name_of_db, but it should’ve been the table’s name.. Such a stupid mistake.. argh