我有Google如何做到这一点,但我无法弄明白。 所以这是我的代码:
<?php
session_start();
Function PrintAll() {
echo "<center>";
echo "Filmpje Nummer: ".$_SESSION['hits'];
echo "</center>";
$dbnaam = "a2943462_Pages";
$connection = mysql_connect($dbhost,$uname,$pass) or die ("niet gelukt om als $user te connecten met $host");
$result = mysql_select_db($dbnaam,$connection);
$sql = "SELECT * FROM PagesInfo WHERE ID=".$_SESSION['hits']; // sql select query
$result = mysql_query($sql, $connection);
while ($rij = mysql_fetch_array($result))
{
echo "<center>";
echo "<h3>" . $rij["Title"] . "</h3>";
echo $rij["Video"] . "<br/>";
echo "<p>" ."Posted By: " . $rij["Posted"] . "</p>";
echo "</center>";
}
}
function ShowButtons() {
echo "<html>
<head><title>COUNTER</title>
<link rel='stylesheet' type='text/css' href='style.css'/>
</head>
<body><center>
<form method='get'>
<input type='submit' name='Previous' value='<< Previous'/>
<input type='submit' name='Next' value='Next >>'/>
</form></center>
</body>
</html>";
}
function DisPre() {
echo "<html>
<head><title>COUNTER</title>
<link rel='stylesheet' type='text/css' href='style.css'/>
</head>
<body><center>
<form method='get'>
<input type='submit' name='Next' value='Next >>'/>
</form></center>
</body>
</html>";
}
if($_GET){
$pageShow = $_SESSION['hits'];
if(isset($_GET['Next'])){
$_SESSION['hits']++;
PrintAll();
}elseif(isset($_GET['Previous'])){
if ($_SESSION['hits'] < 1) {
echo "Cannot go further Back";
DisPre();
}else{
$_SESSION['hits']--;
PrintAll();
}
}
}
if(isset($_SESSION['hits'])){
if ($_SESSION['hits'] < 1){
disPre();
}else{
ShowButtons();
}
}else{
$_SESSION['hits']=1;
PrintAll();
}
?>
那么我如何以及在何处添加执行此操作的代码:
如果它没有在Table PagesInfo中找到任何行,那么它将回显:
echo "<center><img src='Sourceher' height='390px' width='640px' /></center>";
也许有人可以将代码放在我的代码中? 非常感谢任何帮助。
答案 0 :(得分:1)
更改此部分:
$sql = "SELECT * FROM PagesInfo WHERE ID=".$_SESSION['hits']; // sql select query
$result = mysql_query($sql, $connection);
while ($rij = mysql_fetch_array($result))
{
echo "<center>";
echo "<h3>" . $rij["Title"] . "</h3>";
echo $rij["Video"] . "<br/>";
echo "<p>" ."Posted By: " . $rij["Posted"] . "</p>";
echo "</center>";
}
为:
$sql = "SELECT * FROM PagesInfo WHERE ID=".$_SESSION['hits']; // sql select query
$result = mysql_query($sql, $connection);
if(mysql_num_rows($result) >0){
while ($rij = mysql_fetch_array($result))
{
echo "<center>";
echo "<h3>" . $rij["Title"] . "</h3>";
echo $rij["Video"] . "<br/>";
echo "<p>" ."Posted By: " . $rij["Posted"] . "</p>";
echo "</center>";
}
}
else {
echo "<center><img src='Sourceher' height='390px' width='640px' /></center>";
}
N.B: mysql_*
已被弃用PDO
或mysqli_*
。
答案 1 :(得分:0)
以下代码将通过$stmt->num_rows
检查返回的行数来帮助您
它还会使用MySQL
扩展名替换已弃用的 MySQLi
扩展名
此外,它使用prepared queries
保护您免受SQL注入。
$connection = new mysqli($host, $user, $password, $dbnaam);
if (mysqli_connect_errno()){
die("Niet gelukt om als $user te connecten met $host");
}
$query = "SELECT * FROM PagesInfo WHERE ID=?";
if ($stmt = $connection->prepare($query)) {
$stmt->bind_param("i", $_SESSION["hits"]);
$stmt->execute();
$result = $stmt->get_result();
if ($stmt->num_rows == 0){
echo "<center><img src='Sourceher' height='390px' width='640px' /></center>";
}
else{
while ($rij = $result->fetch_array(MYSQLI_NUM)) {
echo "<center>";
echo "<h3>" . $rij["Title"] . "</h3>";
echo $rij["Video"] . "<br/>";
echo "<p>" ."Posted By: " . $rij["Posted"] . "</p>";
echo "</center>";
}
}
$stmt->close();
}