PDO that connects, but INSERT(ing) data does not work:
Code: Select all
$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 ***/
/*** close the database connection ***/
$dbh = null;
The includes file sucessfully connects to the database with the PDO code, but does not add specific data into the animals table. I get the Error:
SQLSTATE[HY000]  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??