1

I converted the following working code:

<?php
    include('config.php');
    $link = mysql_connect($db_host, $username, $password);
    mysql_select_db($db_name);

    $id= $_POST["uniqi"];
    $comments= $_POST["comments"];
    $comments= mysql_real_escape_string($comments);
    $comments = strip_tags($comments);

    $update = "UPDATE mastertable SET comments = '$comments' WHERE id_pk= '$id'";
    mysql_query($update, $link);
    mysql_close();
    header('Location: ccccc.com/pabrowser/… Updated');
?>

to PDO:

<?php
    $id= $_POST["uniqi"];
    $comments= $_POST["comments"];
    $conn = new PDO("mysql:host=$db_host;dbname=$db_name", $username, $password);

    $sql = "UPDATE mastertable SET comments=?    WHERE id_pk=?";
    $q = $conn->prepare($sql);
    $q->execute(array($comments, $id));

    header('Location: ccccc.com/pabrowser/… Updated');
?>

Which gave me

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)' in /home/content/58/9508458/html/pabrowser/comsumcompro.php:4 Stack trace: #0 /home/content/58/9508458/html/pabrowser/comsumcompro.php(4): PDO->__construct('mysql:host=;dbn...', NULL, NULL) #1 {main} thrown in /home/content/58/9508458/html/pabrowser/comsumcompro.php on line 4

1
  • 1
    have you included config.php in the PDO part? Commented Jul 8, 2012 at 16:39

4 Answers 4

4

You've forgotten include('config.php');

Sign up to request clarification or add additional context in comments.

Comments

3

Your $db_host or $db_name value are wrong or you are supplying invalid credentials.

Comments

1

Please add

include('config.php');

Comments

0

Your connection string is wrong or the MySQL server is not responding. Use a try ... catch construction, like this:

include('config.php');

$id = $_POST["uniqi"];
$comments = $_POST["comments"];
try {
    $conn = new PDO("mysql:host=$db_host;dbname=$db_name", $username, $password);
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

And if you will see "Connection failed" message, you understand what's wrong.

1 Comment

Shouldn't the try statement be used with curly brackets?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.