0

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?

2
  • Is $this->HH->Database Mysqli or PDO? Commented Aug 8, 2012 at 1:05
  • new mysqli($server, $user, $pass, $db); Commented Aug 8, 2012 at 1:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I actually was not intending to move over to PDO... nor do I really know much about it at all. I realize the older mysql_ command does not work. Any other alternatives to the PDO approach?
@Greg Actually I think that using PDO is easier than using the old functions. It might be different, but the flexibility is amazing - and if you just want to run simple queries directly it lets you do that as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.