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

无法在PHP中打印传递的数组,php,arrays,Php,Arrays,我有3个文件: php-它处理所有数据库。 php-包含一个函数,用于返回数据库中的最新条目。 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->latestBo

我有3个文件:

php-它处理所有数据库。 php-包含一个函数,用于返回数据库中的最新条目。 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);

    }

}

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

    }

}

?>

DB.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);

    }

}

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

    }

}

?>

我注意到一些问题:

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

    • 我注意到一些问题:

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

      运行此线路时,您与数据库没有连接:

       $result = mysql_query("SELECT url, title FROM site ORDER BY id DESC");
      
      您需要调整代码以将查询字符串发送到DB实例进行处理。您可以向DB中添加一个方法来执行mysql_查询,并将查询从Site::latestBookmarks()传入like,如下所示:

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

      运行此行时,您没有与数据库的连接:

       $result = mysql_query("SELECT url, title FROM site ORDER BY id DESC");
      
      您需要调整代码以将查询字符串发送到DB实例进行处理。您可以向DB中添加一个方法来执行mysql_查询,并将查询从Site::latestBookmarks()传入like,如下所示:

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

      你回来干什么?它到底输出了什么吗?错误怎么办?它打印什么?是空白的吗?或者你会得到“Array()”?如果你使用var_export($site->latestBookmarks());会显示什么?你回来干什么?它到底输出了什么吗?错误怎么办?它打印什么?是空白的吗?或者你会得到“Array()”?如果你使用var_export($site->latestBookmarks());会显示什么?还没有检查各种mysql_*()调用的错误条件,特别是在查询调用上。@Marc B非常正确,这正是我第一眼看到的。也没有检查各种mysql_*()调用的错误条件,特别是在查询调用上。@Marc B非常正确,这正是我第一眼看到的。