我正在尝试使用我想要的查询遇到一些问题。我正在做的是尝试为多个页面创建一个模板,以显示与该页面相关的特定图像。
我有单独的页面,我在其中分配一个变量来定义页面(您可以在我的代码中使用$page
查看我在哪里执行此操作)。然后在我的数据库中,在solution
下,我将一个单独的页面名称命名为特定记录。例如:如果我在数据库列solution
下命名了一个页面“Ball”,我会将一些记录命名为Ball
。
然后在我的查询中,我正在尝试计算与$page
匹配的记录数。如果记录计数大于0,我想在我的else
语句中显示代码。
截至目前,我的数据库连接正常。我没有收到任何错误。你可以看到我的echo $solution_count;
。这显示为0,但我的else-statement
正在运行,这是有道理的。
我是否在尝试计算记录时出错?有谁知道为什么这不起作用?
数据库表 - 显示创建表
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
单个页面上的代码:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$page = "enclosures";
include_once("projectSlider.php");
?>
母版页 - 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();
}
答案 0 :(得分:0)
一个项目有很多滑块,为此你应该有两个表项目和有关系的projectliders。
项目表:
CREATE TABLE `projects` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`project_name` varchar(50) NOT NULL
)
projectsliders:
CREATE TABLE `projectsliders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`solution` varchar(50) NOT NULL,
`image` text NOT NULL,
`alt` text NOT NULL,
`project_id` int(11),
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
为一个项目获取projectSliders,主页 - projectSlider.php
为一个项目获取projectliders的最佳方法是使用OOP,你可以调用一个方法并传递项目id和方法应该返回一个包含projectliders的数组用于这个项目,但我正在改进你的代码。
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 * FROM projectsliders ps inner join projects p
on p.id = ps.project_id
WHERE p.project_name = '. $project_page .'";
if ($project_stmt = $con->prepare($sql_project)) {
$project_stmt->execute();
$count = project_stmt->rowCount();
if( $count != 0 ){
$project_rows = $project_stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($project_rows as $project_row) {
$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 .'">';
echo '<section id="solProj">';
echo '<div class="projSlide">';
echo $project_img;
echo '</div>';
echo '</div>';
}
}else{
echo 'No projects found.';
}
}
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
个别网页:
error_reporting(E_ALL);
ini_set('display_errors', 1);
$project_page = "enclosures";
include_once("projectSlider.php");
我希望这可以帮助你,享受编码。