函数prepare()对非对象错误

时间:2013-07-30 02:30:33

标签: php mysql pdo fatal-error

我已经为此查找了错误,我想我之前正在调用该语句以便初始化它。我已经创建了一个简单的连接类,我可以将其包含在我将与mysql服务器通信的所有文件中。知道我对事物的看法,我很可能在思考问题。我似乎无法找到我做错了什么......

代码的顶部部分被截断,因为它只包含HTML头和php启动代码,这对于此非常重要。

//include database connection
include('connection.php');

$action = isset($_GET['action']) ? $_GET['action']: "";

if($action=='delete'){ //if the user clicked ok, run our delete query
    try {

        $query = "DELETE FROM sc_steamgames WHERE appid = ?";
        $stmt = $con->prepare($query);
        $stmt->bindParam(1, $_GET['appid']);

        $result = $stmt->execute();
        echo "<div>Record was deleted.</div>";

    }catch(PDOException $exception){ //to handle error
        echo "Error: " . $exception->getMessage();
    }

}

//select all data
$query = "SELECT * FROM sc_steamgames";
$stmt = $con->prepare( $query );
$stmt->execute();

//this is how to get number of rows returned
$num = $stmt->rowCount();

echo "<a href='add.php'>Create New Record</a>";

if($num>0){ //check if more than 0 record found

    echo "<table border='1'>";//start table

        //creating our table heading
        echo "<tr>";
            echo "<th>AppID</th>";
            echo "<th>Title</th>";
            echo "<th>Release Date</th>";
            echo "<th>Last Updated</th>";
        echo "</tr>";

        //retrieve our table contents
        //fetch() is faster than fetchAll()
        //http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            //extract row
            //this will make $row['firstname'] to
            //just $firstname only
            extract($row);

            //creating new table row per record
            echo "<tr>";
                echo "<td>{$appid}</td>";
                echo "<td>{$title}</td>";
                echo "<td>{$releasedate}</td>";
                echo "<td>{$lastupdate}</td>";
                echo "<td>";
                    //we will use this links on next part of this post
                    echo "<a href='edit.php?id={$appid}'>Edit</a>";
                    echo " / ";
                    //we will use this links on next part of this post
                    echo "<a href='#' onclick='delete_user( {$appid} );'>Delete</a>";
                echo "</td>";
            echo "</tr>";
        }

    echo "</table>";//end table

}else{ //if no records found
    echo "No records found.";
}

?>

<script type='text/javascript'>

    function delete_user( appid ){
        //this script helps us to

        var answer = confirm('Are you sure?');
        if ( answer ){ //if user clicked ok
            //redirect to url with action as delete and id to the record to be deleted
            window.location = 'index.php?action=delete&id=' + appid;
        } 
    }
</script>

</body>
</html>

connection.php

/* Database Info */

// Host/IP
$host = "localhost";

// Database Name
$db_name = "**";

// Username
$username = "**";

//Password
$password = "**";
/* End Database Info */



try {
        $con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
}catch(PDOException $exception){ //to handle connection error
        echo "Connection error: " . $exception->getMessage();
}

0 个答案:

没有答案