我正在尝试使用PDO从mysql数据库中检索数据,但是" DATABASE NOT FOUND"输出正在执行
这是我的代码
<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;
?>
我无法找到错误
请帮帮我
答案 0 :(得分:2)
在您的代码中,您使用以下语句创建连接对象:
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", "$dbuser", "$dbpass");
但是,为了检查它,你有:
if ($db_found) {
相反,你必须在那里使用$db
。像这样:
if ($db) {
此外,您的执行函数应如下所示:
$add->execute(array(':id'=>$id));
请注意:id
而不是id
。