我有这个PHP代码,可以从我的查询中创建一个表。在此表中,我想基于输出(路径)创建链接。单击这些时,它们将触发另一个查询运行。请参阅我的代码和下面的进一步说明。
代码如下所示:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "DB1");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM testDB";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>file</th>";
echo "<th>path</th>";
echo "<th>type</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['file'] . "</td>";
echo "<td>" . $row['path'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
因此,在运行时,我得到一个包含文件,路径,类型的输出的表。我现在要做的是将路径作为表中的链接,单击时,此查询应该执行一个新表:SELECT * FROM testDB WHERE path = 'PATH FROM THE LINK'.
我试图通过使用预准备语句来实现这一点,将我的代码更改为:
if (isset($_GET["path"])) {
$sql1 = "SELECT `file`, `path`, `type` FROM testDB WHERE path=?";
$stmt = $link->prepare($sql1);
$stmt->bind_param("s", $_GET["path"]);
$result = $stmt->execute();
} else {
$sql = "SELECT * FROM testDB";
$result = $link->query($sql);
}
if ($result) {
if ($result->num_rows) {
然后在表格中添加:
echo "<td><a href='http://myurl.com/test.php?path=" . $row['path'] . "'>" . $row['path'] . "<a/></td>";
但是点击应该执行SELECT file, path, type FROM testDB WHERE path=?
的链接总是给出&#34;找不到与您的查询匹配的记录。&#34;可能是什么问题呢?此外,请注意我在路径中有反斜杠,但是由于准备好的声明,这已经逃脱了,对吧?任何帮助表示赞赏!
答案 0 :(得分:0)
尝试一步一步
$stmt->execute();
$result = $stmt->fetch(); // or fetchAll for multiple arrays
$connection = null;
我正在使用它,所以试试吧......