对于任何人可能正在为这个概念而努力,这是我到目前为止所提出的。我确信有这样做的更干净的方法,但它现在似乎工作正常。由于我也试图将更多面向对象的编码融入到我的一揽子技巧中,因此我最终将解决方案的业务端放入功能中。如果我在重建我的网站时积累了更多的内容,我可能会最终将它们组合到一起,形成一个PDO包装类,或者其他类似的东西。
这里是我的功能:
function fieldCount($field,$condition,$table)
{
//This is just a file that holds the database info and can be whatever name you want
require('database.ini');
try{
$dbh = new PDO("mysql:host=$db_hostname;dbname=$db_database", $db_username, $db_password);
//This turns on the error mode so you get warnings returned, if any
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT $field, COUNT(*) AS count FROM $table $condition GROUP BY $field");
// I tried to bind my $field variable but for some reason it didn't work so I
// commented it out below until I can look deeper into it. If anyone sees any
// glaring errors, please point them out. It looks right to me, but as I said,
// it's not working.
// $stmt = $dbh->prepare("SELECT :field, COUNT(*) AS count FROM $table $condition GROUP BY :field");
// $stmt->bindParam(':field', $field);
$stmt->execute();
$results=$stmt->fetchAll(PDO::FETCH_ASSOC);
// Creates an array similar as the following:
// $results[0][$field] $results[0]['count']
// $results[1][$field] $results[1]['count']
// $results[2][$field] $results[2]['count']
// with each row as an array of values within a numeric array of all rows
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
return $results;
}
的database.ini文件看起来如此简单的东西:
<?php
//database.ini
$db_hostname = 'myHostname';
$db_database = 'databaseNameHere';
$db_username = 'YourUserNameHere';
$db_password = 'YourPasswordHere';
?>
这里是你如何调用该函数:
<?php
// First you have to "require" your file where the function is located,
// whatever you name it.
require('FieldCountFunction.php');
// Determine what column in your database you want to get a count of.
// In my case I want to count how many items there are in each individual
// category, so the $field is Category here.
$field = 'Category';
// If there is a condition to your query, you would state it here.
// In my case I wanted to exclude any items in my Inventory table that
// are marked "Discontinued" in the Notes column of my database.
$condition = "WHERE Notes NOT LIKE 'Discontinued'";
$DB_Table = 'Inventory';
// Now you call the function and enter the $field you want to get a count of,
// any conditions to the query, and the database table name you are querying.
// The results you collected in the function (it was called $results above),
// will be dumped into a new array now called "$fieldArray" below.
$fieldArray = fieldCount($field, $condition, $DB_Table);
// To get the data out again you can do the following to cycle through the
// numeric array of rows (that's what the "$i" is for) and from each numbered
// row you can retrieve the "$field" and "count" that you need. Then display
// them however you want. I'll just echo them out here.
$i=0;
while($i<count($fieldArray))
{
echo $fieldArray[$i]["$field"].' : '.$fieldArray[$i]['count'].'<br />';
$totalCount = $totalCount + $fieldArray[$i]['count'];
$i++;
}
echo $totalCount.' items total';
?>
这将给出类似于以下的结果:
个
配件:638个
标记:432个
面具:146个
包:93
油漆:47个
1356项目总
我希望这可以帮助一个人在那里。如果任何人都可以阐明为什么我的绑定不起作用,我会很感激。如果没有,我相信我会最终弄清楚。
来源
2012-04-15 01:20:01
Tam
你会在PDO中使用相同的查询,并像其他任何查询一样获取结果。没有什么特别的提取'项目的多少个实例'..这些都是mysql和PDO的结果。 '$ pdo-> query('SELECT COUNT()as count FROM myTable') - > execute() - > fetch(PDO :: FETCH_OBJ) - > count;' – 2012-04-08 02:03:55
感谢您的快速评论,Mike。我见过几个不引用fetch()的页面,因为它只返回一行。他们倾向于说要么把它放在while循环中,要么使用fetchAll()。有人说根本不使用它们,而是使用query()。因此,我对所有不同的观点有些混淆。我一直在继续寻找方向,昨天可能会提示如何继续。我会试一试,如果它能正常工作,可以回传代码,以便其他人在需要时可以看到它。 – Tam 2012-04-11 18:20:53
因为我的查询只会返回一行。你的查询('SELECT category,count(*)as count ...')会为每个唯一类别返回两个字段和一行。 – 2012-04-11 18:28:00