在循环中查询pdo内部

时间:2014-11-04 15:39:45

标签: php mysql pdo

大家好我认为我设计的pdo mysql类非常糟糕,因为我无法在另一个查询的while循环中放入查询,因为循环内的新查询消除了旧查询,是否有一个简单的解决这个问题的方法?

我已经习惯了你在查询中使用的真正旧式的php / mysql,然后在fetch($ query)中分配那个你没有用PDO做的查询。这让我感到困惑,这让我困扰了太久。

我的代码切入相关部分

class mysql
{
    // the query counter
    public $counter = 0;

    // the database connection
    public $database;

    // statement handler
    public $STH;

    // store all the queries for debugging
    public $queries = '';

    public function __construct($database_host, $database_username, $database_password, $database_db)
    {
        $options = array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
        ); 
        $this->database = new PDO("mysql:host=$database_host;dbname=$database_db", $database_username, $database_password, $options);  
        $this->database->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
    }

    // A plain query
    public function pquery($sql, $page = NULL)
    {
        global $core;

        try
        {
            $this->STH = $this->database->prepare($sql);  

            $this->counter++;

            $this->queries .= "<br />$sql";

            return $this->STH->execute();
        }

        catch (Exception $e)
        {
            $core->message("Something went wrong. The admin will be notified and punished muhahaha for I am an evil overlord...I'm sure I will be fixed soon.", NULL, 1);
            $this->pdo_error($e->getMessage(), $page, $sql);
        }
    }

    // the main sql query function
    public function sqlquery($sql, $objects = array(), $page = NULL, $referrer = NULL)
    {
        global $core;

        try
        {
            $this->STH = $this->database->prepare($sql);

            foreach($objects as $k=>$p)
            {
                // +1 is needed as arrays start at 0 where as ? placeholders start at 1 in PDO
                if(is_numeric($p))
                {
                    // we need to do this or else decimals always seem to end up 'x.00', pdo has no decimal check, odd
                    if ($this->contains_decimal($p) == true)
                    {
                        $this->STH->bindValue($k+1, $p, PDO::PARAM_STR);
                    }

                    else
                    {
                        $this->STH->bindValue($k+1, (int)$p, PDO::PARAM_INT);
                    }
                }
                else
                {
                    $this->STH->bindValue($k+1, $p, PDO::PARAM_STR);
                }
            }

            $this->counter++;

            $this->queries .= "<br />$sql";

            return $this->STH->execute();
        }

        catch (Exception $e)
        {
            $core->message("Something went wrong. The admin will be notified and punished muhahaha for I am an evil overlord...I'm sure I will be fixed soon.", NULL, 1);
            $this->pdo_error($e->getMessage(), $page, $sql, $referrer);
        }
    }

    public function fetch()
    {
        $this->STH->setFetchMode(PDO::FETCH_ASSOC); 
        return $this->STH->fetch();
    }

0 个答案:

没有答案