1

我正在尝试使用 PDO 从 mysql 数据库中检索数据,但“未找到数据库”输出正在执行

这是我的代码

<form id="form" action="sum1.php" method="post">
<td><p align="center"> IDNO : <input type="text" name="id" id="id" maxlength="10"></p></td>
 <input type="submit" id="submit" class='btnExample' value="Click here to get your Result">
 </form> 
<?PHP 
$dbhost     = "localhost";
$dbname     = "demo";
$dbuser     = "admin";
$dbpass     = "123456";
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", "$dbuser", "$dbpass");
if ($db_found) {
$id = $_POST['id'];

$add = $db->prepare("SELECT htno, SUM(tm) AS tech FROM hmm WHERE htno > :id");
$add -> execute(array('id'=>$id));
echo " <center><table id='mytable' cellspacing='0'  border=3 align=center> 
<tr><TH scope='col'>Total Marks</TH> </tr><center>"; 
while ($row1 = $add->fetch(PDO::FETCH_ASSOC))
{
 echo "<tr>"; 
 echo "<td align=center>" . $row1['tech4']. "</td>";
 echo "</tr>";
  }
  }
else {
print "Database NOT Found ";
}
$db = null;
?>

我找不到错误

请帮我

4

1 回答 1

2

在您的代码中,您正在使用以下语句创建连接对象:

$db = new PDO("mysql:host=$dbhost;dbname=$dbname", "$dbuser", "$dbpass");

但是,为了检查它,你有:

if ($db_found) {

相反,你必须在$db那里使用。像这样:

if ($db) {

编辑

此外,您的执行函数应如下所示:

$add->execute(array(':id'=>$id));

注意:id代替id

于 2012-10-15T06:37:59.207 回答