I am using MySQL and PHP 5.3 and tried this code.
$dbhost = \'localhost\';
$dbuser = \'root\';
$dbpass = \'\';
$con = mysql_connect(\"localhost\", \"root\",
I had the same trouble and found out after trying out 1000000 different options that the default mysql engine still remains MyISAM.
Verifying that InnoDB is the Default Storage Engine as such:
Issue the command SHOW VARIABLES LIKE 'have_innodb';
to confirm that InnoDB is available at all.
Then issue the command SHOW ENGINES;
to see all the different MySQL storage engines.
Look for DEFAULT
in the InnoDB
line and NOT in the MyISAM
line. I realized that my provided had a setting preventing me from changing the default engine using SET storage_engine=MYISAM;
. In my case, I contacted my provider and was directed to where I could change the option to be able to change the default engine. Hope this helps!
The possible way to insert Special characters using PDO, just follow the these to steps:
1) Set the Attribute to you connection class.
$this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAME'utf8'");
2) Now you can use htmlspecialchars while query creation and insertion to database:
$ShortDescription = htmlspecialchars($_POST['ShortDescription'], ENT_QUOTES);
$LongDescription = htmlspecialchars($_POST['LongDescription'],ENT_QUOTES);
And it will work fine.
I suggest always connecting to a database via PDO.
Below is a sample code;
<?php
class connMysql {
protected static $instance;
private static $database_type = "mysql";
private static $hostname = "localhost";
private static $user = “database_user”;
private static $password = “database_user_password”;
private static $database_name = “your_database”;
private static $persistent = false;
private function __construct() {
try {
self::$instance = new \PDO(self::$database_type . ":host=" . self::$hostname . ";dbname=" . self::$database_name
, self::$user
, self::$password
, array(
\PDO::ATTR_PERSISTENT => self::$persistent,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_STRINGIFY_FETCHES => true,
\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"
));
} catch (PDOException $e) {
echo "Connection Error: " . $e->getMessage();
}
}
public static function getInstance() {
if (!self::$instance) {
new connMysql();
}
return self::$instance;
}
public static function close() {
self::$instance = null;
}
}
You're missing an S: it's SET NAMES and not SET NAME
:
$this->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
You also need to un-comment it of course. Also, PDO::MYSQL_ATTR_INIT_COMMAND
can not be set with PDO::setAttribute() after you've established your database connection (the constant name says it all), you've to specify it in the constructor using the $driver_options
argument, like this:
$this->db = new PDO($this->dsn, $this->username, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
An alternative to this is to just execute that very same query immediately after connecting:
$this->db = new PDO($this->dsn, $this->username, $this->password);
$this->db->exec("SET NAMES 'utf8';");