我的PDO构造错误-PHP [英] Error on my PDO Construct - Php

查看:56
本文介绍了我的PDO构造错误-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是第6行的抱怨:Warning: PDO::__construct() expects parameter 2 to be string, array given

伴随第7行错误:

Fatal error: Call to a member function prepare() on a non-object in

我该如何解决?我已经测试过查询,并且工作正常.

How do I fix this? I've tested the query and it works fine..

<?php

## Loop through results from mysql
try{
    #connection string
        $dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thedb',array(PDO::ATTR_PERSISTENT => true));
        $q = $dbconn->prepare("SELECT thecol FROM thetbl");
    #call stored proc
        $q->execute();
    #get the rows into an array
        $result = $q->fetchAll();
        foreach($result as $r){
            $xmlUrl = $r['FW_ArtSrcLink'];
            $ConvertToXml = simplexml_load_file($xmlUrl);
            # -> Setup XML
            $newsStory = $ConvertToXml->channel->item;
        }   

    # -----> Load News Stories
        for($i = 0;$i<sizeof($newsStory); $i++){

    # Source of Article Info-->
            $SrcTitle=$newsStory[$i]->title;
            $SrcLink=$newsStory[$i]->link;

    # Actual News Article Info -->
            $title=$newsStory[$i]->title;
            $desc=$newsStory[$i]->description;

    # Output Results ------------>      
            echo '<hr>';
            echo '<strong>'.'Title:'.$title.'</strong>'.'(via: <a href=\''.$SrcLink.'\'>'.$SrcTitle.'</a>'.'<br />';
            //echo 'Link:'.$link.'<br />';
            echo 'Description'.$desc.'<br>';
            echo '<hr>';
        }
} // try

catch(Exception $e){

    $errorStored = $e->getMessage() . ' on ' .'/errors/fanwire_loop.php';  #where errors are stored
    $pageDateOfError = '/aggregate_looping.php'.date('l jS \of F Y h:i:s A'); #inc the file and date into the file too
    file_put_contents($errorStored,$pageDateOfError, FILE_APPEND | LOCK_EX);
} // catch

#Load in File


/*************************************************



$xmlUrl ="http://sports.espn.go.com/espn/rss/mlb/news";
$ConvertToXml = simplexml_load_file($xmlUrl);



# -> Setup XML
$newsStory = $ConvertToXml->channel->item;

# -----> Load News Stories
for($i = 0;$i<sizeof($newsStory); $i++){

    // Source of Article Info-->
    $SrcTitle=$newsStory[$i]->title;
    $SrcLink=$newsStory[$i]->link;

    // Actual News Article Info -->
    $title=$newsStory[$i]->title;
    $desc=$newsStory[$i]->description;


    echo '<hr>';
    echo '<strong>'.'Title:'.$title.'</strong>'.'(via: <a href=\''.$SrcLink.'\'>'.$SrcTitle.'</a>'.'<br />';
    //echo 'Link:'.$link.'<br />';
    echo 'Description'.$desc.'<br>';
    echo '<hr>';
}

***********************************************/

?>

推荐答案

您错误地初始化了PDO对象,构造函数的第二个参数应该是用户名,而不是选项数组.

You're initializing the PDO object incorrectly, the second parameter of the constructor should be the username, not an array of options.

$dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thedb',array(PDO::ATTR_PERSISTENT => true));

应该是

$dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thedb',
                  'yourusername',
                  'yourpassword',
                  array(PDO::ATTR_PERSISTENT => true));

请参见 PDO的PHP手册页:: __ construct()有关更多信息.

您遇到的第二个错误是因为第一个错误未能正确创建$dbconn对象.

The second error you're getting because the $dbconn object wasn't created properly due to the first error.

这篇关于我的PDO构造错误-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆