tags:

views:

36

answers:

1

How to rewrite this in pdo:

 $con = mysql_connect("localhost:".$LOCAL_DB_PORT, $LOCAL_DB_USER, $LOCAL_DB_PASS);
 mysql_select_db("xnews", $con);
 mysql_query("set names utf8", $con);

Thanks in advance!

+2  A: 

What about something like this :

$db = new PDO('mysql:dbname=xnews;host=localhost;port=' . $LOCAL_DB_PORT, $LOCAL_DB_USER, $LOCAL_DB_PASS);
$db->query('set names utf8');


To open a connection, you have to instanciate PDO, using its constructor, which receives a DSN as first parameter.

And, then, to send queries, you can use the PDO::query method.


Or, for the UTF-8 part, maybe you could use the fourth parameter to PDO::__construct, like this :

$db = new PDO('mysql:dbname=xnews;host=localhost;port=' . $LOCAL_DB_PORT, 
          $LOCAL_DB_USER, 
          $LOCAL_DB_PASS, 
          array(PDO::MYSQL_ATTR_INIT_COMMAND =>  "SET NAMES 'UTF8'")
      );

See the list of specific stuff for the MySQL Driver, amongst which there is this one :

PDO::MYSQL_ATTR_INIT_COMMAND

Command to execute when connecting to the MySQL server. Will automatically be re-executed when reconnecting.

Pascal MARTIN