【发布时间】:2013-11-21 12:57:47
【问题描述】:
我是 PDO 的新手,并尝试尽我所能使用它我已经编写了下面的代码,但我收到一个错误,为 foreach() 提供的参数无效。
这是我的代码 -
<?php
function sqlConnect()
{
$mysqlConnection = new PDO('mysql:host=localhost;dbname=students', "root", "root");
return $mysqlConnection;
}
function getServerResults($costvm)
{
$sql = "SELECT * FROM ".$costvm;
$mysqlConnection = sqlConnect();
$resultSet = $mysqlConnection->query($sql);
return $resultSet;
}
?>
这是写在错误页面上的代码。
try
{
$resultSet = getServerResults("servers");
foreach($resultSet as $row)
{
echo $row['servername']."<br/>";
}
} catch(PDOException $e)
{
echo 'ERROR: '.$e->getMessage();
}
?>
【问题讨论】:
-
您得到的是空结果吗?检查这个问题stackoverflow.com/questions/16382572/…>
-
is_array($resultSet) 是否等于 true?
-
返回
$mysqlConnection->query($sql);的内容可能对 us3.php.net/pdo.query 有帮助