我想从数据库中选择一个我从$ _GET获得的变量值,但它没有显示任何结果。任何人都可以帮我找到我的代码有什么问题吗?
这是我的第一页如何获得GET的价值:
<?php
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><a href="file.php?subject=<?= $row['subject'] ?>"><?= $row['subject'] ?></a></td>
<td><?= $row['location'] ?></td>
<td><?= $row['geo'] ?></td>
<td><?= $row['date'] ?></td>
<td><?= $row['piority'] ?></td>
</tr>
<?php
}
}
?>
这是我的第二页,我想从第一页的主题变量中获取数据库中的数据:
<?php
$varPage = $_GET['subject'];
$servername = "localhost";
$username = "bayansh_user";
$password = "u)nHf,Accmo)";
$dbname = "bayansh_bmc";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn,"SELECT `date` FROM `editor` WHERE subject = '.$varPage.'");
while($row = mysqli_fetch_array($result))
?>
现在我想在这里写日期是我的代码:
<p style="font-family:B Zar; direction:rtl; font-size:165%;"> <?= $row['date'] ?> </p>
但它什么都没写。 我的代码有什么问题吗?
答案 0 :(得分:1)
您的SQL似乎存在疏忽。你有一个额外的点,这可能是一个错字。尝试解决该部分,您的代码最有可能按预期工作。这里的一些人建议使用Prepared Statement(这是非常必要的),在这种情况下,这个Snippet将使用PDO而不是MySQLi ......虽然原则并没有那么大的不同。
<?php
$varPage = $_GET['subject'];
$servername = "localhost";
$username = "bayansh_user";
$password = "u)nHf,Accmo)";
$dbname = "bayansh_bmc";
$dbh = null;
// USING PDO INSTEAD::
$dsn = 'mysql:host=' . $servername . ';dbname=' . $dbname;
try {
$dbh = new PDO($dsn, $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
// USING PREPARED STATEMENT... NOTICE "=:SBJ"
$sql = "SELECT `date` FROM `editor` WHERE subject=:SBJ";
// USING PREPARED STATEMENT CONTINUED: NOTICE "$dbh->prepare()"
$stmt = $dbh->prepare($sql);
// PASSING VALUES: NOTICE "['SBJ'=>$varPage]"
$stmt->execute( array('SBJ'=>$varPage) );
$resultSet = $stmt->fetchAll(PDO::FETCH_OBJ);
// LOOP THROUGH THE RESULT-SET AND DO SOME THINGS WITH THE DATA...
foreach($resultSet as $intKey=>$objData){
var_dump($objData->date); //<== DUMP SOME DATA TO THE STREAM
}
答案 1 :(得分:0)
您应该使用以下代码
$result = mysqli_query($conn,"SELECT `date` FROM `editor` WHERE subject = '".$varPage."'");