这是代码:
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 在 index.php 的开头被调用。然后,稍后在 index.php 我有这一行:
<?php print_table($con->get_db()); ?>
为什么会这样?
谢谢!