显示mysql db-fetch_object()错误的值

时间:2013-11-19 16:10:08

标签: php

我将表单中的值提交到mysql db中。之后,我正在执行选择查询以尝试获取先前插入的值并显示其结果。我使用while循环迭代并只显示两个值academy_id和academy name。尝试显示此值时,我收到错误Call to undefined method mysqli_stmt::fetch_object()。如何修复此错误并显示所需的值? Example

if(isset($_POST['submit'])) {


$db_insert  = $db_con->prepare("INSERT INTO academy (name, type, status, academy_id, street_address, zipcode, city, state, comments) VALUES (?,?,?,?,?,?,?,?,?)"); 
$db_insert->bind_param('sssisssss', $_POST['name'], $_POST['type'], $_POST['status'], $_POST['acad_id'], $_POST['street'], $_POST['zipcode'], $_POST['city'], $_POST['state'], $_POST['acad_comm']);
$db_insert->execute();

$academy_id = $_POST['acad_id'];



 //Query to Read Values from database based on academy_id 
$db_select  = $db_con->prepare("
SELECT a.name AS 'Academy Name:', 
       a.academy_id AS 'Academy ID:',
       a.status AS 'STATUS:',
       a.type AS 'Type:',
       a.street_address AS 'Street:',
       a.city AS 'City:',
       a.state AS 'State:',
       a.zipcode AS 'Zip Code:',
       a.comments AS 'Comments:'
FROM academy a
WHERE a.academy_id = $academy_id
");
$db_select->bind_param("i", $academy_id);
$db_select->execute();

while ($row = $db_select->fetch_object()) {
    echo "<h2>Result:</h2><br>";
    echo "Academy ID: " . $academy_id. "<br>";  
    echo "Name: " . $_POST['name'] . "<br>";        
} 
}

?>
<form action="test9.php" method="POST">
        Name: <input type="text" name="name"></br> 
        Academy Status:
        <select>
          <option value="ACTIVE">ACTIVE</option>
          <option value="INACTIVE">INACTIVE</option>
        </select>
        Type:
        <select>
          <option value="Upper-Secondary">Upper-Secondary</option>
          <option value="Post-Secondary">POST SECONDARY</option>
        </select>

        Courses being offered?
        <select name="courses_offered">
            <option value="1">1</option>
            <option value="2">2</option>
        </select>
        <div id="course_catalog"></div>
        Academy ID: <input type="text" id="acad_id" name="acad_id"></br>
        Address: <input type="text" id="street" name="street"></br>
        Zip Code: <input type="text" id="zip_input" name="zipcode"></br>
        City: <input type="text" id="city" name="city" value=""></br>
        State: <input type="text" id="state" name="state" value=""></br>
        Overall Notes/Comments:</br><textarea id="acad_comm" name="acad_comm" rows="4" cols="50"></textarea></br>

    <input value="SAVE" name="submit" type="submit">
</form> 

2 个答案:

答案 0 :(得分:1)

错误消息对我来说很准确:mysqli_stmt类没有我能发现的任何fetch_object()方法。

请查看一些usage examples。您不应该从语句中获取结果,而是从返回的mysqli_result object获取结果:

$db_insert->execute();

您根本没有进行任何错误检查。这应该失败:

$db_select->bind_param("i", $academy_id);

...因为查询不包含任何占位符:

WHERE a.academy_id = $academy_id

....应该是:

WHERE a.academy_id = :academy_id

但是你没有检查任何结果值,所以你最终会猜测。

答案 1 :(得分:0)

你可以尝试fetch或fetchAll而while和foreach都可以

while ($row = $db_select->fetch(PDO::FETCH_ASSOC)) {
    echo "<h2>Result:</h2><br>";
    echo "Academy ID: " . $academy_id. "<br>";  
    echo "Name: " . $_POST['name'] . "<br>";        
} 

foreach ($db_select->fetchAll(PDO::FETCH_ASSOC) as $row) {
    echo "<h2>Result:</h2><br>";
    echo "Academy ID: " . $academy_id. "<br>";  
    echo "Name: " . $_POST['name'] . "<br>";   
}