尝试为我的项目获取列,从未在 PDO 中这样做过,尝试查看文档,但它并没有真正帮助。
$sql = $CONNECT_TO_DATABASE->query("SELECT COUNT(*) FROM comments");
$result = $sql->fetch(PDO::FETCH_ASSOC);
$r = $result->fetchColumn();
我做错了什么?
致命错误:在第 6 行的 C:\xampp\htdocs\project\events.php 中的非对象上调用成员函数 fetchColumn()
$r = $result->fetchColumn();
谢谢!
联系:
<?php
/**
* Connect to Database
* @Author Jony <artemkller@gmail.com>
**/
##################CHANGE THIS####################
/** #
* MYSQL HOST HERE (Example: localhost) #
**/ #
#
DEFINE ("MYSQL_HOST", "localhost"); #
#
/** #
* MYSQL USER HERE (Example: root) #
**/ #
#
DEFINE ("MYSQL_USER", "root"); #
#
/** #
* MYSQL PASSWORD HERE (Example: password) #
**/ #
#
DEFINE ("MYSQL_PASSWORD", ""); #
#
#################################################
# Connect to the database using PDO.
$CONNECT_TO_DATABASE = new PDO('mysql:host='.MYSQL_HOST.';dbname=project', MYSQL_USER, MYSQL_PASSWORD);
# See if theres an error while connecting to the database
try {
$CONNECT_TO_DATABASE->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $ERROR_MESSAGE)
{
echo 'ERROR: ' . $ERROR_MESSAGE->getMessage();
}
?>