将MYSQL连接转换为PDO

时间:2015-01-26 10:49:49

标签: php mysql pdo

我一直试图使用mysql运行我的存储过程很长一段时间。每当我使用下面的代码时

$link_id = DbConnection::getInstance('mycon')->connectMysql();

$table_count = mysql_query("SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'mycon' AND table_name LIKE 'table_%' ")

while($row = mysql_fetch_array($table_count)){
$table = $row["TABLE_NAME"];
     $excute = mysql_query("dummy_2('$table')") or die(mysql_error());
     $result = mysql_fetch_assoc($excute);
     var_dump($result);
}

它出错了

Commands out of sync; you can't run this command now

因此我试图通过 PDO 来完成这项工作..然而,由于我是新手,我仍然会收到很多错误。

我尝试过的代码。

$pdo =new PDO(DbConnection::getInstance('mycon')->connectMysql());
    if($pdo->exec("call dummy_2('$table')"))
    {
        echo "its working";

    }

任何机构都可以帮助我通过PDO完成上述连接和查询

连接

$link_id = DbConnection::getInstance('mycon')->connectMysql();

相关脚本

protected $dbConfig;

protected $dbConfigName;

protected $mysql;
protected function __construct($dbConfigName){
    $this->dbConfigName = $dbConfigName;
    $this->dbConfig = Config_Reader::read($dbConfigName, 'db_con.ini')->database;
}

public static function getInstance($dbConfigName){
    if (!isset(self::$instances[$dbConfigName])){
        self::$instances[$dbConfigName] = new self($dbConfigName);
    }
    return self::$instances[$dbConfigName];
}

public function connectMysql(){
    if (!is_resource($this->mysql)){
        $this->mysql = mysql_connect($this->dbConfig->host, $this->dbConfig->username, $this->dbConfig->password, true);
        if (!$this->mysql){
            return false;
        }
        if (mysql_select_db($this->dbConfig->name, $this->mysql)){
            return $this->mysql;
        }
        else{
            mysql_close($this->mysql);
            $this->mysql = null;
            return false;
        }
    }
    return $this->mysql;
}

}

0 个答案:

没有答案