Home | Programming | SQL | SQL -> PDO Cheatsheet
RSS Githublogo Youtubelogo higgs.at kovacs.cf

The original PHP MYSQL extionsion is going to be removed stepwise with PHP version 5.5/5.6. The functions will be depreciated the next version and in the future they will be removed completely due to security reasons.

Alternatives are the mysqli and PDO_MYSQL.

Below the old MYSQL commands and some corresponding PDO_MSQL commands:

MYSQL PDO_MySQL
connect to the DB

$db_link = mysql_connect (
     "MYSQL_HOST",
     "MYSQL_USER",
     "MYSQL_PASSWORD"
);

...

mysql_close($db_link); //terminate the connection
connect to the DB

$dsn = "mysql:host=$host; dbname=$db_name; charset=utf8";

$opt = array(
      PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);

$pdo = new PDO($dsn, $db_name, $pdo_pw, $opt);

...

$pdo = null; //terminate the connection
count the number of lines in a table

$lines_query = mysql_query("SELECT COUNT(COLUMNAME) FROM TABLE");
$line_amount = mysql_fetch_row($lines_query); $line_amount = $line_amount[0];

echo $lines_amount." lines";
count the number of lines in a table

$count_lines = $pdo->query('SELECT * FROM TABLE');
$lines_count = $count_lines->rowCount();

echo $lines_count.' lines';