PHP MySQL查询无法使用我的数据库类[关闭]

PHP MySQL查询无法使用我的数据库类[关闭]

问题描述:

I've set up a Database class, a User class and a UserTools class to interact with a MySQL database. It seems that my UPDATE, DELETE and INSERT commands are working find but I can't figure out what's going on with my SELECT statements. I'm only using mysql right not until I get everything working as I'd like, then I'll switch over to mysqli or PDO. But anyway, here's what I'm looking at....

DB class....

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);
}

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

    $resultArray = array();

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

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


    return $resultArray;
}

UserTools class

public function getUser($id) {
    $db = new DB();
    $result = $db->select('users', 'id = $id');

    return new User($result);
}

There seems to be an issue with how I'm processing the rows or something. I've followed similar setups with UPDATE,DELETE,INSERT and they seem to work fine but I don't know whats going on here.

If I call an instance of new UserTools(), then try to run the getUser() function, it's not returning anything the way it's set up. If I keep the result from being pushed through the User class constructor, it just returns a Reference Value, meaning that I'm not processing the rows properly.

I have a feeling I'm overlooking something stupid here so any help would be greatly appreciated. Thanks!

For a start,

$result = $db->select('users', 'id = $id');

Should be

$result = $db->select('users', 'id = '.$id);

As Casimir mentioned, there's a typo in public function processsRowSet

I'd echo $sql; die; to check if the query is complete.

In UserTools class: 'id = $id' wouldn't parse in $id. Instead do "id = {$id}" or similar so that it can parse $id.