Greg
Greg

Reputation: 1055

procedural php/mysql to object oriented issue

This is the original code I am trying to write in object oriented form..

$posResult = mysql_query("SELECT MAX(position)+1 FROM todo");

        if(mysql_num_rows($posResult))
            list($position) = mysql_fetch_array($posResult);

This is where I am at.... I think I need to bind_result? also, I have an issue with the mysql_fetch_array...

if($stmt = $this->HH->Database->prepare("SELECT MAX(position)+1 FROM todo")) {
        $stmt->execute();
        $stmt->store_result();

        if($stmt->num_rows != FALSE) {
            list($position) = mysql_fetch_array($posResult);
        }
    }

Can someone advise on how to correct this?

Upvotes: 0

Views: 226

Answers (1)

Fluffeh
Fluffeh

Reputation: 33522

It looks like you are moving over to PDO, which is great, it is a much better way to connect, but you are still trying to use some of the older mysql_* commands which won't work. While there are a number of ways to get the data, but this is the approach that I most commonly use:

if($stmt = $this->HH->Database->prepare("SELECT MAX(position)+1 FROM todo")) {
    $stmt->execute();

    $stmt->setFetchMode(PDO::FETCH_INTO, new yourClassName);
    foreach($stmt as $yourClassObject)
    {
        $someVar=$yourClassObject->QueryColumnName;
        // or
        $this->someProperty=$yourClassObject->QueryColumnName;
    }
    }

I do generally have a class that matches the results of the query, so I use FETCH_INTO but you can use a different method such as return an indexed array, associated array or others.

Upvotes: 1

Related Questions