-1

这是代码:

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()); ?>

为什么会这样?

谢谢!

4

2 回答 2

2
function print_table($db)
{
    $aRows = $db->query("SELECT * FROM name_of_db");
    foreach ($aRows as $row)
    {
        echo $row['title'];
    }
}

这解决了问题。不要mysql_query在 foreach 中使用 IN,而是使用返回的值

于 2012-08-21T18:27:52.437 回答
0

我修好了它。问题是我使用的是 SELECT * FROM name_of_db,但它应该是表的名称..真是个愚蠢的错误..啊

于 2012-08-21T20:51:54.517 回答