所以我试图在PHP中运行查询,而查询本身没有错误(或者看起来似乎如此),编辑器在“echo”语句中看到错误。代码是这样的:
<?php
include("include/session.php");
?>
<?php
$db = new PDO('mysql:host=localhost;dbname=cvtool;charset=utf8', 'user', 'pass'); // change these to your own database details
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // later, change ERRMODE_WARNING to ERRMODE_EXCEPTION so users wont see any errors
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$department = isset($_GET['department'])? $_GET['department']: null;
$sql = 'SELECT *
FROM education
WHERE school LIKE ?;
$q = $conn->prepare($sql);
$q->execute(array('%$department%');
$q->setFetchMode(PDO::FETCH_ASSOC);
while ($r = $q->fetch()) {
echo sprintf('%$department', $r['school']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!--The viewport tag is used in order to scale the page properly inside any screen size -->
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>CV Tool</title>
<link rel="shortcut icon" href="images/favicon.ico" />
<link rel="stylesheet" href="css/main.css"/>
<!--Import JQuery from stored file -->
<script src="js/jquery-1.11.1.min.js"></script>
<!--Import JQuery from Google's Content Delivery Network -->
<!--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">-->
<link href='http://fonts.googleapis.com/css?family=PT+Sans:400,700' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="js/menu.js"></script>
<script type="text/javascript" src="js/backToTop.js"></script>
</head>
<body>
<!--Big wrapper contains the whole site (header,navigation menu,....,footer-->
<div id="big_wrapper">
<header id="top_header">
<a href="main.php"><img src="images/cvlogo.png"> </a>
</header>
<br>
<nav class="clearfix">
<ul class="clearfix">
<li><a href="main.php">Home</a></li>
<?php
/**
* User has already logged in, so display relavent links, including
* a link to the admin center if the user is an administrator.
*/
if($session->logged_in){
echo "<li><a href=\"search.php\">Search</a></li>"
."<li><a href=\"myCVs.php\">My CV(s)</a></li>"
."<li><a href=\"userinfo.php?user=$session->username\">My Account</a></li>"
;
echo "<li><a href=\"process.php\">Logout</a></li>";
}
else
?>
</ul>
<a href="#" id="pull">Menu</a>
</nav>
<section id="main_section">
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Department</th>
</tr>
</thead>
<tbody>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($r['school'])?></td>
</tr>
<?php endwhile; ?>
</section>
<footer id="the_footer">
City CV Tool 2014
</footer>
<a href="#" class="back-to-top"></a>
</div>
</body>
</html>
问题在于无论我如何更改它,它仍然会给我一个错误。错误发生在以下特定行:
while ($r = $q->fetch()) {
echo sprintf('%$department', $r['school']);
}
错误可能只是缺少的东西,或者我没有意识到的额外的东西。我知道代码是关于一个非常具体的案例,但仍然感谢任何帮助。
答案 0 :(得分:2)
您永远不会关闭以下字符串变量:
$sql = 'SELECT *
FROM education
WHERE school LIKE ?;