我想使用上一个和下一个按钮来使用ID滚动数据库。当我还在使用mysql时,这是我的代码。现在它被改为mysqli所以我不知道这段代码是否仍然有用,因为我一直都是null。
function getNavID($id) {
$result4= mysqli_query("SELECT
( SELECT id FROM products_list
WHERE id > '$id' LIMIT 1 ) AS nextValue,
( SELECT id FROM products_list
WHERE id < '$id' ORDER BY id DESC LIMIT 1 ) AS prevValue
FROM products_list
LIMIT 1");
if ($resultID = mysqli_fetch_array($result4)) {
return $resultID;
}
else {
return NULL;
}
}
$LinkID = getNavID($id);
if (!is_null($LinkID['prevValue']))
{
?>
<a href="update.php?id=<?php echo urlencode($LinkID['prevValue']); ?>" target="_top">Previous</a>
<?php
}
else if (!is_null($LinkID['nextValue']))
{
?>
<a href="update.php?id=<?php echo urlencode($LinkID['nextValue']); ?>" target="_top">Next</a>
<?php
}
else
{
echo "No Entries";
}
除了将mysql_query更改为mysqli_query之外,还有什么我需要更改的吗?在此先感谢您的帮助!
答案 0 :(得分:-1)
试试这个:
global $pdo;
$id = $the_selected_id;
$stmt_a = $pdo->prepare("
(SELECT * FROM images WHERE id < ? ORDER BY id DESC LIMIT 1)
UNION (SELECT * FROM images WHERE id = (SELECT MAX(id) FROM images)) LIMIT 1");
$stmt_b = $pdo->prepare("
(SELECT * FROM images WHERE id > ? ORDER BY id ASC LIMIT 1)
UNION (SELECT * FROM images WHERE id = (SELECT MIN(id) FROM images)) LIMIT 1");
// $vars = array(':id' => $id);
$prev = $stmt_a->execute(array( (int)$id ));
$next = $stmt_b->execute(array( (int)$id ));
if ($stmt_a->rowCount() > 0) {
while($row = $stmt_a->fetch(PDO::FETCH_ASSOC)) {
echo '<a href="' . $row['prev_id'] . '">Previous</a>';
}
} else {
echo 'no previous';
}
if ($stmt_b->rowCount() > 0) {
while($row = $stmt_b->fetch(PDO::FETCH_ASSOC)) {
echo '<a href="' . $row['next_id'] . '">Next</a>';
}
} else {
echo 'no next';
答案 1 :(得分:-1)
将这样的db conection转换为sentax,如下所示:
<?
//enter your MySQL database host name, often it is not necessary to edit this line
$db_host = "localhost";
//enter your MySQL database username
$db_username = "your user name";
//enter your MySQL database password
$db_password = "your passwor";
//enter your MySQL database name
$db_name = "your db name";
//URL to the the site
$ScriptPath = "http://www.your-site.com/";
$db = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error($db));
mysqli_select_db($db, $db_name) or die("Error " . mysqli_error($db));
function getNavID($id) {
$result4= mysqli_query($db, "SELECT
( SELECT id FROM products_list
WHERE id > '$id' LIMIT 1 ) AS nextValue,
( SELECT id FROM products_list
WHERE id < '$id' ORDER BY id DESC LIMIT 1 ) AS prevValue
FROM products_list
LIMIT 1");
if ($resultID = mysqli_fetch_array($db, $result4)) {
return $resultID;
}
else {
return NULL;
}
}
$LinkID = getNavID($id);
if (!is_null($LinkID['prevValue']))
{
?>
<a href="update.php?id=<?php echo urlencode($LinkID['prevValue']); ?>" target="_top">Previous</a>
<?php
}
else if (!is_null($LinkID['nextValue']))
{
?>
<a href="update.php?id=<?php echo urlencode($LinkID['nextValue']); ?>" target="_top">Next</a>
<?php
}
else
{
echo "No Entries";
}
?>