好的,所以这里有一些我已经为你们调试过的信息。
我已经读过 PDO SQL同时不允许多个查询...但这不是一个查询吗?如果不是,我想我必须回到旧版本。
这里是查询(使用echo从PHP文件打印)。
SELECT `Date`, SUM(`Amount`), `Quantity Sold` FROM Transactions WHERE Date BETWEEN DATE_SUB(CURDATE(), INTERVAL 15 DAY) AND CURDATE() GROUP BY Date ORDER BY Date DESC
其中,在PHPMYADMIN上返回:
Showing rows 0 - 6 (7 total, Query took 0.0004 sec) [Date: 2014-05-26 - 2014-05-13]
这是代码。
try {
$count=0;
$statement = $SQLConnection->prepare('
SELECT `Date`, SUM(`Amount`), `Quantity Sold` FROM ' . $SQLConfig["SQL_DATA_TABLE"] . '
WHERE Date BETWEEN DATE_SUB(CURDATE(), INTERVAL 15 DAY) AND CURDATE()
GROUP BY Date ORDER BY Date DESC');
$statement->execute();
//echo "<tr><td>test</td><td>test</td><td>test</td></tr>";
foreach($statement as $row) {
echo "Row: " . $count;
$count++;
}
} catch (PDOException $e) {
die("Error!: " . $e->getMessage() . "<br/>");
}
我也试过添加一行:
$statement->setFetchMode(PDO::FETCH_BOTH);
然后将foreach更改为
while($row = $statement->fetch()){
但是,它仍然没有返回任何行。
我决定最后一次回到另一个系统,同时保持未经编辑的查询,并使用以下内容完美地运行:
mysql_select_db($SQLConfig["SQL_DATABASE"])or die("Error: Cannot locate database");
$statement = 'SELECT `Date`, SUM(`Amount`), `Quantity Sold` FROM ' . $SQLConfig["SQL_DATA_TABLE"] . '
WHERE Date BETWEEN DATE_SUB(CURDATE(), INTERVAL 15 DAY) AND CURDATE()
GROUP BY Date ORDER BY Date DESC';
$results = mysql_query($statement);
while($row = mysql_fetch_array($results)) {
使用SQLi尝试了相同的代码并返回零结果。到底是什么:/我试图转换我的项目以确保其安全并在必要时使用准备好的陈述,但这使得它非常困难。
使用Sqli的代码
if($statement = $SQLConnection->prepare('
SELECT `Date`, SUM(`Amount`), `Quantity Sold` FROM ' . $SQLConfig["SQL_DATA_TABLE"] . '
WHERE Date BETWEEN DATE_SUB(CURDATE(), INTERVAL 15 DAY) AND CURDATE()
GROUP BY Date ORDER BY Date DESC'))
{
$statement->execute();
$statement->bind_result($result);
$statement->fetch();
echo "Row: " .$result;
}
根据要求,$ SQLConnection是什么:
<?php
$configuration = include_once 'Configuration.php';
$PDOConnection = new PDO('mysql:host' .$Configuration["SQL_HOST"]. ';dbname='.$Configuration["SQL_DATABASE"], $configuration["SQL_USERNAME"], $configuration["SQL_PASSWORD"]);
$NormalConnection = mysql_connect($Configuration["SQL_HOST"], $Configuration["SQL_USERNAME"], $Configuration["SQL_PASSWORD"]) or die("Error: Failure to connect to host.");
$SQLiConnection = new mysqli($Configuration["SQL_HOST"], $Configuration["SQL_USERNAME"], $Configuration["SQL_PASSWORD"], $configuration["SQL_DATABASE"]);
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
}
?>
$ SQLConnection实际上是我代码中的一个参数,因此根据我测试的数据库,我将传递$ PDOConnection,$ NormalConnection或$ SQLiConnection
示例:
createIndex($PDOConnection, $Configuration);
function createIndex($SQLConnection, $SQLConfig) {