2

オプションの引数とともに、データベース内のテーブルから行を取得するクラス関数を作成しました。これは単一の行を取得するときに機能しますが、複数の行が返されるときにこれを機能させることはできません。

これがUsersクラスの内容です

public function getUsers($filter="") {
    $Database = new Database();
    if($filter == 'male') 
        $extra = "WHERE gender = 'm'";

    $sql = "SELECT *
            FROM users 
            $extra";

    if ($Database->query($sql))
        return $Database->result->fetch_assoc();
    else
        return false;
}

データベース クラス

class Database {

    private $db = array();
    private $connection;
    private $result;

    public function __construct() {
        $this->connect();
    }

    public function connect() {
        $this->connection = mysqli_connect('mysql.com', 'username', 'pass');
        mysqli_select_db($this->connection, 'database');
    }

    public function query($sql) {
        $this->result = mysqli_query($this->connection, $sql);
        return $this->result;
    }

これは、行を表示しようとするために使用されるコードです

if ($student = $User->getUsers($filter)) {

    echo "<table>\n";   
    echo "<tr><td>Col 1</td><td>Col 2</td><td>Col 3</td><td>Col 4</td><td></td><td></td></tr>";

    foreach($student as $row) {       
       echo "<tr>";
        echo "<td>$row[col1]</td>";   
        echo "<td>$row[col2]</td>";   
        echo "<td>$row[col3]</td>";   
        echo "<td>$row[col4]</td>";   
        echo "<td>$row[col5]</td>";   
        echo "<td>$row[col6]</td>";   
       echo "</tr>\n";         
    }

    echo "</table>";        
    }

(私はOO PHPを学んでいます、ご容赦ください)

4

3 に答える 3

0

I'm still learning OOP but I have used this:

    protected $connection, $result, $_numRows;

    public function query($sql)
    {
    $this->result = mysql_query($sql, $this->connection);
    $this->_numRows = mysql_num_rows($this->result);
    }


    /*
    * @desc    get result af query
    *
    * @returns string $result
    */
    public function getResult()
    {
    return $this->result;
    }

    /*
    * @desc     Return rows in table
    * @returns int $_numRows
    */
    public function numRows()
    {
    return $this->_numRows;
    }

    /*
    * @desc     Count rows and add to array
    * @return string $rows array
    */
    public function rows()
    {
    $rows =array();
    for ($x=0; $x < $this->numRows(); $x++) {
        $rows[] = mysql_fetch_assoc($this->result);
    }
    return $rows;
    }

Then you could use something like this to get the rows:

public function getUsers($filter="") {
    $Database = new Database();
    if($filter == 'male') 
        $extra = "WHERE gender = 'm'";

    $sql = "SELECT *
            FROM users 
            $extra";
        if ($this->numRows() == 0) {
            echo "No rows found";
        } else {

        foreach ($this->rows() as $b) {
            $c = array('something' => $b['col']);
        }
        return $c;
    }

Modify the last part to suit your needs.

于 2013-04-28T03:35:20.357 に答える
0

なんで$result->fetch_assoc()2回使ってるの?あなたは配列を返しています..あなたは対処する必要があります$student.

于 2013-04-28T03:22:00.883 に答える
0

コードのどこ$resultから来たのかは明確ではありません...

以下によって返されるオブジェクトの fetch_assoc() メソッドを呼び出す必要があります。

$mysqli->query($query) 
于 2013-04-28T03:18:52.467 に答える