0

I have looked up the error for this and I think I am calling the statement before for it to be initialized. I have made a simple connection class that I can include into all of my files that will be talking to the mysql server. Knowing how I am with things, I am most likely over thinking things. I cant seem to find what I am doing wrong.....

Top part of the code is cut off as it only contains the HTML head and php starting code that is non-important for this.

//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();
}
Ryahn
  • 537
  • 7
  • 24
  • 1
    What does `var_dump($con)` say? – Barmar Jul 30 '13 at 02:32
  • Well, looks like `$con` is not an object (DBMS connection attempt failed). Test with `var_dump($con);`. – BlitZ Jul 30 '13 at 02:33
  • @Barmar @CORRUPT $con is in my connection.php. If I run var_dump, I get `object(PDO)#1 (0) { }` – Ryahn Jul 30 '13 at 02:37
  • You must either have a typo, or some code you're not showing us, that reassigns `$con`. Which line is getting the error? – Barmar Jul 30 '13 at 02:39
  • Line 32 which is `$stmt = $con->prepare( $query );`. I will edit the page to show the connection.php. – Ryahn Jul 30 '13 at 02:41
  • var_dump($con) right inside of your try block. – Orangepill Jul 30 '13 at 04:03
  • As Barmar said, there must be something you're not showing us. When I run your code, it functions as expected. In what you posted, `$stmt = $con->prepare( $query );` is not line 32, it's line 24, so something is missing. – Nick Coons Jul 30 '13 at 08:09

0 Answers0