Php mysql#u result($result$i,';id';)等效于mysqli
我想将我的代码从Php mysql#u result($result$i,';id';)等效于mysqli,php,mysql,mysqli,Php,Mysql,Mysqli,我想将我的代码从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;
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++){
//确保PHP不会试图显示不存在的结果
如果($i==$total\u结果){
打破
}
//将每行的内容回显到表中
$bookid=mysql_result($result,$i,'id');
$cover=mysql_result($result,$i,'cover');
回声“
”;
}
回声“”;
我想把它转换成那样的东西
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>";
for($i=$start;$i<$end;$i++){
//确保PHP不会试图显示不存在的结果
如果($i==$total\u结果){
打破
}
$i;
$book\u row=mysqli\u fetch\u assoc($result);
$book_id=$book_行[“id”];
$book_cover=$book_row[“cover”];
$book_title=$book_行[“title”];
$book_auther1=$book_行[“auther1”];
$book_auther2=$book_行[“auther2”];
回声“
”;
}
回声“”;
但是
mysql
代码中变量$i
中的问题我不知道我将把它放在mysqli
的哪个位置,或者类似这样的东西(来自您的原始代码):
$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]);
谢谢你的回答,但我希望代码是过程式的,$i变量,而不是我用于分页的id,我将是计数器,计数一result@AhmedAbuelmagd:已更新答案以处理分页。感谢您的回答,但我希望代码采用过程样式和$i变量,而不是我使用它的id分页一些结果,我将计数器计数一个结果可能你正在寻找这样的东西:我更新了我的答案基于上述链接。
<?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>";
?>