开发者

Unable to print a passed array in PHP

I have 3 files:

DB.class.php - which handles all the databases. Site.class.php - contains a function to return the latest entries in a database. index.php - trying to print the array passed.

I am trying to pass an array into index.php from Site.class.php which is using a function from DB.class.php to put the mysql results into an associative array.

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 ($d开发者_JAVA百科ata 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();

    }

}

?>


A few problems I noticed:

  • You run a query in function latestBookmarks before you connect to the db;
  • In your function connect you connect to a database, but the result is discarded immediately, $connection is lost as soon as the function finishes.


You have no connection to the database when you run this line:

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

You will need to adjust your code to send the query string to the DB instance for processing. You can add a method to DB to execute mysql_query, and pass the query in like from Site:: latestBookmarks() like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜