0

I'm having trouble getting this to work w/out getting "Call to undefined method PDO::execute() in C:\xampp\htdocs\bookmarks\index.php on line 5"

<?php

    function addBookmark($url, $conn){
        $conn->prepare('INSERT INTO entries (url) VALUES (:url)');
        $conn->execute(array(':url' => $url));
    }

    try {
        $conn = new PDO('mysql:dbname=bookmarks;host=localhost', 'username', 'password');
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        if(isset($_POST['bookmark'])) {
            addBookmark($_POST['bookmark'], $conn);
        }

        $results = $conn->query('select * from bookmarks.entries');

    } catch (exception $e) {
        die($e->getMessage());
    }

?>

I just started fooling around with PDO today so I don't have the best grasp on the concept. Any help would be greatly appreciated.

Tim Aych
  • 1,365
  • 4
  • 16
  • 34
  • 1
    What errors are you getting? – Pekka May 12 '13 at 17:19
  • Instead of setting exception mode *after* connecting, [set it at the time of connect](http://stackoverflow.com/questions/15990857/reference-frequently-asked-questions-about-pdo#15990858). Also, do not use try/catch to die error message. it's pointless and insecure – Your Common Sense May 12 '13 at 17:28
  • 2
    HTTP Error 500 is not an error itself but just a stub. You need to read the error log for the actual error. Most likely it wold be irrelevant to PDO – Your Common Sense May 12 '13 at 17:35
  • Thank you, checking the log, I'm getting " Call to undefined method PDO::execute() in C:\xampp\htdocs\bookmarks\index.php on line 5" – Tim Aych May 12 '13 at 17:54

3 Answers3

2

The connection string seems a bit wrong.

'mysql:bookmarks'

should also have a host; and bookmarks is what I think the database name!

'mysql:dbname=bookmarks;host=localhost'

$conn->prepare('INSERT INTO entries (url) VALUES (:url)');
$conn->execute(array(':url' => $url));

needs to be rewritten as follows:

$stmt = $conn->prepare('INSERT INTO entries (url) VALUES (:url)');
$stmt->execute(array(':url' => $url));
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • Thanks, I changed the connection parameters accordingly. I'm still, however, getting the error. – Tim Aych May 12 '13 at 17:27
  • @TimAych As commented by *Pekka 웃* in your question; please show what errors you are receiving! – hjpotter92 May 12 '13 at 17:28
  • Yeah, edited the error into original post.. "HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request." – Tim Aych May 12 '13 at 17:32
2
function addBookmark($url, $conn){
    $stmt = $conn->prepare('INSERT INTO entries (url) VALUES (?)');
    $stmt->execute(array($url));
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

I dont know exactly, but the way i create connection string is

mysql:host=localhost;database=database
mysql:host=localhost;dbname=database

I dont know if there is any other workaround. I forgot whether its database or dbname

Aditya Heartly
  • 873
  • 1
  • 6
  • 8