这是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
我该如何解决?我已经测试了该查询,它工作正常..
<?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 对象,构造函数的第二个参数应该是用户名,而不是选项数组。
$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));
See the PDO::__construct() 的 PHP 手册页 http://www.php.net/manual/en/pdo.construct.php了解更多信息。
您收到的第二个错误是因为$dbconn
由于第一个错误,对象未正确创建。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)