无法在PHP中打印传递的数组

时间:2011-05-18 15:16:52

标签: php arrays

我有3个文件:

DB.class.php - 处理所有数据库。 Site.class.php - 包含一个返回数据库中最新条目的函数。 index.php - 尝试打印传递的数组。

我试图将一个数组传递给Site.class.php中的index.php,它使用DB.class.php中的函数将mysql结果放入一个关联数组中。

的index.php:

<?php

// index.php

include 'classes/Site.class.php';
$site = new Site();

print_r($site->latestBookmarks());

?>

Site.class.php:

<?php 

// Site.class.php

require_once 'DB.class.php';

class Site {

    function latestBookmarks() {

        $result = mysql_query("SELECT url, title FROM site ORDER BY id DESC");

        $db = new DB();

        return $db->processRowSet($result);

    }

}

?>

DB.class.php:

<?php 

// DB.class.php

class DB {

    protected $db_name = "project";
    protected $db_user = "root";
    protected $db_pass = "root";
    protected $db_host = "localhost";

    // Open up a connection to the database.

    public function connect() {

        $connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
        mysql_select_db($this->db_name);

        return true;

    }

    // Takes a MySQL row and returns an associative array where the keys in the array are the column names in the row set.

    public function processRowSet($rowSet, $singleRow=false) {

        $resultArray = array();

        while ($row = mysql_fetch_assoc($rowSet)) {
            array_push($resultArray, $row);
        }

        if ($singleRow === true)
            return $resultArray[0];

        return $resultArray;

    }

    // Select rows from the database.

    public function select($table, $where) {

        $sql = "SELECT * FROM $table WHERE $where";

        $result = mysql_query($sql);

        if (mysql_num_rows($result) == 1)
            return $this->processRowSet($result, true);

        return $this->processRowSet($result);

    }

    // Update a current row in the database.

    public function update($data, $table, $where) {

        foreach ($data as $column => $value) {
            $sql = "UPDATE $table SET $column = $value WHERE $where";
            mysql_query($sql) or die(mysql_error());
        }

        return true;

    }

    // Insert a new row into the database.

    public function insert($data, $table) {

        $columns = "";
        $values = "";

        foreach ($data as $column => $value) {
            $columns .= ($columns == "") ? "" : ", ";
            $columns .= $column;
            $values .= ($values == "") ? "" : ", ";
            $values .= $value;
        }

        $sql = "INSERT INTO $table ($columns) VALUES ($values)";

        mysql_query($sql) or die(mysql_error());

        return mysql_insert_id();

    }

}

?>

2 个答案:

答案 0 :(得分:4)

我发现了一些问题:

  • 在连接到db;
  • 之前,您在函数latestBookmarks中运行查询
  • 在您的函数connect中,您连接到数据库,但结果立即被丢弃,一旦函数完成,$connection就会丢失。

答案 1 :(得分:1)

运行此行时,您与数据库没有任何关联:

 $result = mysql_query("SELECT url, title FROM site ORDER BY id DESC");

您需要调整代码以将查询字符串发送到数据库实例进行处理。您可以向DB添加一个方法来执行mysql_query,并像这样传递来自Site :: latestBookmarks()的查询:

 $db = new DB();
 $db->executeQuery("SELECT url, title FROM site ORDER BY id DESC");
 return $db->processRowSet();