我如何使用PDO?

时间:2014-03-03 08:27:12

标签: php mysql pdo

我是PDO编码方式的新手,如何将以下代码更改为PDO:

$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));

2 个答案:

答案 0 :(得分:2)

您必须启用PDO。

$query       = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = $pdo->query($query, PDO::FETCH_COLUMN, 0);

您可以在此处找到所需的一切:http://php.net/pdo

答案 1 :(得分:0)

编辑 例如:

$sql = "SELECT count(*) as num FROM $tableName"; 
$result = $con->prepare($sql); 
$result->execute(); 
$number_of_rows = $result->fetchColumn();