
PDO that connects, but INSERT(ing) data does not work:
Code: Select all
<?php
include 'include/connect.php';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=animals", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
/*** INSERT data ***/
$count = $dbh->exec("INSERT INTO animals(animal_type, animal_name) VALUES ('kiwi', 'troy')");
/*** echo the number of affected rows ***/
echo $count;
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'localhost' (111)
I have done some research and all I could find is that either the mysqld.sock file is in the wrong location (which it isn't) or a PDO field is disabled in php.ini (isn't either).
ugh. I could go on forever. If no one has a solution please feel free to explain what the mysld.sock file does, or why using PDOs is prefered. I assume PDOs help developers avoid making PHP queries/field that are vunerable towards SQL Injection??
