我想将我的代码从mysql
转换为mysqli
。
但是,我在这里遇到了问题。
mysql
代码
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
// echo out the contents of each row into a table
$bookid = mysql_result($result, $i, 'id');
$cover = mysql_result($result, $i, 'cover');
echo "
<div></div>
<li>
<div id='divborder'>
<div id='header'>
<a class='caption' href='page.php?id=$bookid '><img src='$cover' alt='' class='headerBg' /></a>
</div>
</div>
</li> ";
}
echo "</ul></div>";
我想把它转换成类似的东西
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
$i;
$book_row = mysqli_fetch_assoc($result);
$book_id = $book_row["id"];
$book_cover = $book_row["cover"];
$book_title = $book_row["title"];
$book_auther1 = $book_row["auther1"];
$book_auther2 = $book_row["auther2"];
echo "
<div></div>
<li>
<div id='divborder'>
<div id='header'>
<a class='caption caption1'data-title='$data_title' href='book/$book_id'><img src='$book_cover' alt='' class='headerBg' /></a>
</div>
</div>
</li> ";
}
echo "</ul></div>";
但$i
代码中变量mysql
中的问题我不知道我将把它放在mysqli
答案 0 :(得分:-1)
$query = "select * from books";
// run the query
$result = mysqli_query($query);
// fetch all the rows
$arrayofrows = mysqli_fetch_all($result, MYSQLI_ASSOC);
// then you can use $i from your for loop
// var_dump($arrayofrows[$i]);
答案 1 :(得分:-1)
或者类似的东西(来自原始代码):
<?php
$conn = mysqli_connect('host','username','password','database');
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
$query = "select * from tablename where id ='$i'";
$result = $conn->query($query);
$data = mysqli_fetch_all($result,MYSQLI_ASSOC);
$book_id = $data["id"];
$book_cover = $data["cover"];
$book_title = $data["title"];
$book_auther1 = $data["auther1"];
$book_auther2 = $data["auther2"];
echo "
<div></div>
<li>
<div id='divborder'>
<div id='header'>
<a class='caption caption1'data-title='$data_title' href='book/$book_id'><img src='$book_cover' alt='' class='headerBg' /></a>
</div>
</div>
</li> ";
}
?>
<强>更新强>
<?php
$con = mysqli_connect('host','username','password','database');
$per_page=5;
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
//Selecting the data from table but with limit
$query = 'SELECT * FROM books LIMIT $start_from, $per_page';
$result = mysqli_query ($con, $query);
$total_records = mysqli_num_rows($result);
$total_pages = ceil($total_records / $per_page);
while ($row = mysqli_fetch_assoc($result)) {
$book_id = $row["id"];
$book_cover = $row["cover"];
$book_title = $row["title"];
$book_auther1 = $row["auther1"];
$book_auther2 = $row["auther2"];
echo "
<div></div>
<li>
<div id='divborder'>
<div id='header'>
<a class='caption caption1'data-title='$data_title' href='book/$book_id'><img src='$book_cover' alt='' class='headerBg' /></a>
</div>
</div>
</li> ";
}
//pagination goes here:
echo "<center><a href='".htmlspecialchars($_SERVER["PHP_SELF"])."?page=1'>First Page</a>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='".htmlspecialchars($_SERVER["PHP_SELF"])."?page=".$i."'>".$i."</a>";
}
echo "<center><a href='".htmlspecialchars($_SERVER["PHP_SELF"])."?page=$total_pages'>Last Page</a>";
?>