-1

我有这个 OOP Select 我正在使用,我想将 JOIN 添加到功能中。我的新代码虽然没有生成数组。任何人都可以帮忙吗?

这是我最初的简单选择,它像梦一样工作

public function select($table, $rows = '*', $where = null, $order = null)
{
    $q = 'SELECT '.$rows.' FROM '.$table;
    if($where != null)
        $q .= ' WHERE '.$where;
    if($order != null)
        $q .= ' ORDER BY '.$order;

    $query = @mysql_query($q);
    if($query)
    {
        $this->numResults = mysql_num_rows($query);
        for($i = 0; $i < $this->numResults; $i++)
        {
            $r = mysql_fetch_array($query);
            $key = array_keys($r);
            for($x = 0; $x < count($key); $x++)
            {
                // Sanitizes keys so only alphavalues are allowed
                if(!is_int($key[$x]))
                {
                    if(mysql_num_rows($query) > 1)
                        $this->result[$i][$key[$x]] = $r[$key[$x]];
                    else if(mysql_num_rows($query) < 1)
                        $this->result = null;
                    else
                        $this->result[$key[$x]] = $r[$key[$x]];
                }
            }
        }
        return true;
    }
    else
    {
        return false;
    }
}

这是我尝试添加一种使用 join 的方法,但它不返回任何数组。

public function select($table, $rows = '*', $join = null, $where = null, $order = null){
    // Create query from the variables passed to the function
    $q = 'SELECT '.$rows.' FROM '.$table;
    if($join != null){
        $q .= ' JOIN '.$join;
    }
    if($where != null){
        $q .= ' WHERE '.$where;
    }
    if($order != null){
        $q .= ' ORDER BY '.$order;
    }
    // Check to see if the table exists
    if($this->tableExists($table)){
        // The table exists, run the query
        $query = @mysql_query($q);
        if($query){
            // If the query returns >= 1 assign the number of rows to numResults
            $this->numResults = mysql_num_rows($query);
            // Loop through the query results by the number of rows returned
            for($i = 0; $i < $this->numResults; $i++){
                $r = mysql_fetch_array($query);
                $key = array_keys($r);
                for($x = 0; $x < count($key); $x++){
                    // Sanitizes keys so only alphavalues are allowed
                    if(!is_int($key[$x])){
                        if(mysql_num_rows($query) > 1){
                            $this->result[$i][$key[$x]] = $r[$key[$x]];
                        }else if(mysql_num_rows($query) < 1){
                            $this->result = null;
                        }else{
                            $this->result[$key[$x]] = $r[$key[$x]];
                        }
                    }
                }
            }
            return true; // Query was successful
        }else{
            array_push($this->result,mysql_error());
            return false; // No rows where returned
        }
    }else{
        return false; // Table does not exist
    }
}
4

1 回答 1

4

如果你调用这个函数,你应该像这样声明它:

public function select($table, $rows, $where, $order)
{
        // your statements
}

然后这样称呼它:

select('your_table','rows_you_want_select','where_conditions','column_you_want_to_sort');

另一方面,如果您尝试使用 join 语句,请像这样声明它:

public function select($table, $rows, $join, $where, $order)
    {
            // your statements
    }

然后这样称呼它:

select('your_table','rows_you_want_select','join_conditions','where_conditions','column_you_want_to_sort');

例子:

select('table1','id',' table2 on table1.id = table2.id','id = 1','id');

关于函数式编程你还有很多东西要学,请避免使用mysql_. 而是使用mysqli_or PDO。如果您真的追求 OOP,那么我强烈建议您使用PDO

于 2013-06-03T09:31:25.093 回答