1

I'm trying to make my own "Database" class for MySQLi, it's the classic class that coomunicate with database, so into my web application I can do (for example)

$user = $database->select($connection, "SELECT * FROM user");

So I wrote the function (this is the part where there are no parameters in the query)

public function select($link, $query, $types, $arrayValues)
{
    $set = array(); //this is what the function will return
    $statement;

    if($types == null && $arrayValues == null)
    {
        $statement = $link->prepare($query);
        if($statement->execute())
        {
            $statement->store_result();
            $meta = $statement->result_metadata();
            $fileds = array();
            $results = array();

            //put into the array the exact number of variables for call_user_func_array
            while($field = $meta->fetch_field())
            {
                $var = $field->name;
                $$var = null;
                $fields[$var] = &$$var;
            }
            $fieldCount = $statement->field_count;
            call_user_func_array(array($statement,'bind_result'),$fields);

            while($statement->fetch())
            {
                $set[] = $fields;
            }
            echo '<pre>';
            print_r($set);
            echo '</pre>';
        }
    }
    else
    {
         part of the function where there are parameters in the query
    }

    return $set;
}

Now the problem is that $set (which must be returned) is an array, but all the values of the array are the same: the last record I extracted with the query. Why? I'm going crazy

Matt Chad
  • 75
  • 6
  • Does `$statement->fetch()` even fetch more than one row? – rndus2r Oct 12 '17 at 13:10
  • `$set[] = $value;` - what is `$value` ...? Doesn't appear anywhere else in the code shown. – CBroe Oct 12 '17 at 13:11
  • @rndus2r yes, there are two rows returned by the query. – Matt Chad Oct 12 '17 at 13:13
  • @CBroe sorry, I just correct, I failed to paste. It is not $value but $fields – Matt Chad Oct 12 '17 at 13:14
  • 1
    For the love of all that is good, either make use of get_result() or - better - use PDO instead of mysqli. – Your Common Sense Oct 12 '17 at 13:15
  • 1
    http://php.net/manual/en/mysqli-stmt.bind-result.php#92505 – CBroe Oct 12 '17 at 13:16
  • Note that this branch makes no sense at all. Given there are no placeholders in the query, there is no point to struggle with this awful code at all. just run query() and then fetch the data with usual fetch_array(). You will need it with the other branch though. – Your Common Sense Oct 12 '17 at 13:20
  • @Your Common Sense is mysqli->query safe? I mean I can run prepared statements on there? And is PDO safer than Mysqli? – Matt Chad Oct 12 '17 at 13:28
  • Ok forget it, it's too complex for you – Your Common Sense Oct 12 '17 at 13:31
  • Means? Just asking, I can search on google and learn it with ot without you explaining. Thanks for responding anyway. – Matt Chad Oct 12 '17 at 13:34
  • Just take my word: USE PDO. With PDO this function will be 20 times shorter and 100 times more useful. [Example](https://phpdelusions.net/pdo/pdo_wrapper#run). Also please read on the [database class writers' common mistakes](https://phpdelusions.net/pdo/common_mistakes) – Your Common Sense Oct 12 '17 at 13:38
  • Thank you, I will seriously thinking about using PDOinstead of MySQLi. Going to read your link now. – Matt Chad Oct 12 '17 at 13:41
  • You can ask me if you will have any questions but you should start your comment from @your to make me notified. – Your Common Sense Oct 12 '17 at 13:43
  • @YourCommonSense it is too late for my last project, but I'm learning PDO for the next which will be done in PHP too, so :) liking the PDO style and database support ;) – Matt Chad Oct 12 '17 at 14:04
  • Your code works fine as is for me. I get all records extracted, not just the last one. The only thing missing is that you need to declare `null` as default parameter values, or to explicitly pass `null` when calling the method. – Jeff Puckett Oct 12 '17 at 14:48

0 Answers0