在非对象pdo

时间:2016-07-28 14:36:10

标签: php mysql pdo

我尝试将一个部分添加到从表单到数据库表的IMPORT信息中,我收到此错误:

Fatal error: Call to a member function prepare() on a non-object in /srv/disk7/2173760/www/site.net/admin.php on line 79

这是我尝试使用的代码:

<?php
            $pdo = new PDO('mysql:host=;dbname=', '', '');
            $sql = "SELECT * FROM games LIMIT 10";
            foreach ($pdo->query($sql) as $row) {
                              if(isset($_POST['insert']))
                              {        
                           $game_title=$_POST['gtitle']; 
                           $yt_id=$_POST['ytlink'];
                           $name=$_POST['gtitle'];
                           $source=$_POST['slink'];
                           $url=$_POST['keysl'];
                           $steam_id=$_POST['appid'];
                           $categories=$_POST['inlineRadioOptions'];
                           $query_ins="INSERT INTO tbl_games(ytlink,gtitle,slink,keysl,appid,keysleft) VALUES(:yt_id,:name,:source,:url,:steam_id,:categories)";
                           $stmt_query=$dbh->prepare($query_ins);
                           $games_ins=$stmt_query->execute(array(":yt_id"=>$ytlink,":name"=>$gtitle,":source"=>$slink,":url"=>$keysl,":steam_id"=>$appid,":categories"=>$keysleft));
                           if(!$games_ins)
                           {
                           $error=$stmt_query->errorInfo();
                           echo $error['2'];
                           }
                           }
        ?>

这是第79行:

$stmt_query=$dbh->prepare($query_ins);

我替换了这个,但仍然没有工作..

<?php
            $pdo = new PDO('mysql:host=;dbname=', '', '');
            $sql = "SELECT * FROM games LIMIT 10";
            foreach ($pdo->query($sql) as $row) {
                            $query = "INSERT INTO tbl_games(yt_id,name,url,source,keysleft,steam_id)".
                            "SELECT ytlink,gtitle,slink,keysl,appid FROM games LIMIT 10"

          ?>

1 个答案:

答案 0 :(得分:0)

你必须在使用之前实例化$ dbh,就像你使用$ pdo一样。

为什么不使用第二行的对象$ pdo呢?