I am running into some issues with a query I am trying to make. What I am doing is trying to create a template for multiple pages to display specific images that correlate with that page.
I have individual pages where I am assigning a variable to define the page (you can see where I do this in my code with $page
). Then within my database, under solution
I am naming the specific records one of the individual page names. For example: if I named a page "Ball", under the database column solution
, I would name a few records Ball
.
Then within my query, I am trying to count how many records exist that match $page
. If the record count is more than 0, I want to display the code in my else
statement.
As of now, my database connection is working. I am not getting any errors being printed. You can see my echo $solution_count;
. This is showing a 0, but my else-statement
is running, which makes 0 sense.
Am I doing anything wrong with how I am trying to count the records? Does anyone see why this isn't working?
DB Table - show create table
projectslider
CREATE TABLE `projectslider` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`solution` varchar(50) NOT NULL,
`image` text NOT NULL,
`alt` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
Code on the individual pages:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$page = "enclosures";
include_once("projectSlider.php");
?>
Master page - projectSlider.php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = 'localhost';
$username = 'root';
$password = '';
try {
$con = new PDO('mysql:host='.$servername.';dbname=mb', $username, $password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//$page = mysql_real_escape_string($page);
//SQL Call
$sql_project = "
SELECT *, COUNT(solution) AS solution_count
FROM projectslider
WHERE solution = '. $page .'
";
if ($project_stmt = $con->prepare($sql_project)) {
$project_stmt->execute();
$project_rows = $project_stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($project_rows as $project_row) {
$solution_count = $project_row['solution_count'];
echo $solution_count;
$project_solution = $project_row['solution'];
$project_img = $project_row['image'];
$project_alt = $project_row['alt'];
$project_img = '<img class="home-comment-profile-pic" src=" '. $project_img .'" alt="' . $project_alt .'">';
if ($solution_count === 0) {
echo 'No projects found.';
} else {
echo '<section id="solProj">';
echo '<div class="projSlide">';
echo $project_img;
echo '</div>';
echo '</div>';
}
}
}
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}