我想從使用PDO的mysql數據庫中檢索數據,但出現錯誤。使用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");
$id = $_POST['id'];
$add = $db->prepare("SELECT SUM(tech) AS tech4 FROM (
(SELECT SUM(tm) AS tech FROM jbit WHERE htno > :id)
UNION ALL
(SELECT SUM(tm) AS tech FROM hmm WHERE htno > :id))t1");
$add -> execute(array('id'=>$id));
$result3 = $db->query($add);
echo " <center><table id='mytable' cellspacing='0' border=3 align=center>
<tr><TH scope='col'>Total Marks</TH> </tr><center>";
while ($row1 = $result3->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td align=center>" . $row1['tech4']. "</td>";
echo "</tr>";
}
$result3->closeCursor();
$db = null;
?>
錯誤我得到
Warning: PDO::query() expects parameter 1 to be string, object given in /home/nhtsoft/public_html/engineershub/sum1.php on line 17
和
Fatal error: Call to a member function fetch() on a non-object in /home/nhtsoft/public_html/engineershub/sum1.php on line 20
第17行是
$result3 = $db->query($add);
第20行是
while ($row1 = $result3->fetch(PDO::FETCH_ASSOC))
任何想法?
呀做,現在有沒有錯誤,但仍然輸出是空的 – Aryan
@Aryan可能是由於您的查詢空結果。嘗試在phpmyadmin中運行此查詢? – StaticVariable
得到了輸出..謝謝你很多,但顯示不正確的輸出,而使用Mysql查詢我得到確切的輸出,但通過使用PDO我得到錯誤的輸出。請你檢查一次查詢 – Aryan