I was trying to convert my project over to PDO earlier and I couldn’t do it, and I was very upset. I’m just a beginner with PHP (Started a week ago) however, typcially i pick up on things really quickly and I’m able to solve my problems relatively quickly. However, this has had me stumped for 3 days now, I went ahead and finished my entire project using the standard mysql_query function, however…. it’s vulnerable to injection when it comes to my login scripts and I would like to have it transferred over as soon as possible.
[B]Lets get started, this is the code that configures all of my connections.
[code]<?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();
}
?>
I have thoroughly tested each of these connections and I’m positive that they are all connecting to the database successfully.
[B]Here’s my code when using the mysql_query method(This works perfectly):
[code]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)) {
[B]Here’s my code when using the SQLi method(This one returns 0 rows):
[code]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;
}
[B]Here’s my code when using the PDO method(This one returns 0 rows):
[code]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/>”);
}
[B]I have also tried changing the PDO code to use the following but it didn’t work
[code]$statement->setFetchMode(PDO::FETCH_BOTH);
while($row = $statement->fetch()){
The following:
[code]var_dump($statement->fetchAll(PDO::FETCH_ASSOC));
prints out Array(0) { }
For those curios as to where I’m getting $SQLConnection from
[code]function createIndex($SQLConnection, $SQLConfig) {
The connection that I’m using is passed as a parameter.
Example:
[code]createIndex($PDOConnection, $Configuration);
Please help me get PDO working, as it’s my preferred choice.