php - 从sql迁移到mysqli

标签 php mysql sql mysqli pagination

我有一个代码可以在表格中显示数据以及分页,但是代码在mysql中

<?php
include('config.php');    //include of db config file
include ('paginate.php'); //include of paginat page

$per_page = 10;         // number of results to show per page
$result = mysql_query("SELECT * FROM subcategory");
$total_results = mysql_num_rows($result);

$row = mysql_fetch_assoc($result);

$total_pages = ceil($total_results / $per_page);//total pages we going to have

//-------------if page is setcheck------------------//
if (isset($_GET['page'])) {
    $show_page = $_GET['page'];             //it will telles the current page
    if ($show_page > 0 && $show_page <= $total_pages) {
        $start = ($show_page - 1) * $per_page;
        $end = $start + $per_page;
    } else {
        // error - show first set of results
        $start = 0;              
        $end = $per_page;
    }
} else {
    // if page isn't set, show first set of results
    $start = 0;
    $end = $per_page;
}
// display pagination
$page = intval($_GET['page']);

$tpages=$total_pages;
if ($page <= 0)
    $page = 1;

?>

<!doctype html>
<html lang="en">

<head>
</head>
<body>
<?php
    $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;

        // display data in table
        echo "<table class='tablesorter' cellspacing='0'> ";
            echo "<thead>
                        <tr> 
                            <th></th>
                            <th>Subcategory Name</th> 
                            <th>Category Name</th> 
                            <th>Edit</th> 
                            <th>Delete</th> 
                        </tr> 
                  </thead>";

                for ($i = $start; $i < $end; $i++) 
                    {
                        if ($i == $total_results) 
                            {
                                break;
                            }

                            // echo out the contents of each row into a table
                            echo "<tr " . $cls . ">";
                                echo '<td></td>';
                                echo '<td>' . mysql_result($result, $i, 'subcatname') . '</td>';
                                echo '<td>' . mysql_result($result, $i, 'catname') . '</td>';    
                            echo "</tr>";
                    }       
                // close table>
        echo "</table>";
        // pagination

echo '<div class="pagination" style="margin-left:300px;" >';
        if ($total_pages > 1) 
            {
                echo paginate($reload, $show_page, $total_pages);
            }
    echo "</ul>
</div>";
?>
</body>


整个代码在同一页上,现在我试图将其转换为mysqli,并且还为表中的每一行插入编辑和删除功能。

我拥有的新代码是

<?php
include('config.php');    //include of db config file
include ('paginate.php'); //include of paginat page

$per_page = 10;         // number of results to show per page

$result = mysqli_query($con,"SELECT * FROM subcategory ");
$total_results = mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have

//-------------if page is setcheck------------------//
if (isset($_GET['page'])) {
    $show_page = $_GET['page'];             //it will telles the current page
    if ($show_page > 0 && $show_page <= $total_pages) {
        $start = ($show_page - 1) * $per_page;
        $end = $start + $per_page;
    } else {
        // error - show first set of results
        $start = 0;              
        $end = $per_page;
    }
} else {
    // if page isn't set, show first set of results
    $start = 0;
    $end = $per_page;
}
// display pagination
$page = intval($_GET['page']);

$tpages=$total_pages;
if ($page <= 0)
    $page = 1;

?>

<!doctype html>
<html lang="en">

<head>
</head>
<body>

<?php
    $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;

        // display data in table
        echo "<table class='tablesorter' cellspacing='0'> ";
            echo "<thead>
                        <tr> 
                            <th></th>
                            <th>Subcategory Name</th> 
                            <th>Category Name</th> 
                            <th>Edit</th> 
                            <th>Delete</th> 
                        </tr> 
                  </thead>";

                for ($i = $start; $i < $end; $i++) 
                    {
                        if ($i == $total_results) 
                            {
                                break;
                            }

                            // echo out the contents of each row into a table
                            echo "<tr " . $cls . ">";
                                echo '<td></td>';
                                echo '<td>' . mysql_result($result, $i, 'subcatname') . '</td>';
                                echo '<td>' . mysql_result($result, $i, 'catname') . '</td>';

                                echo "<td><a href=\"a_edit_subcategory.php?id=".$row['id']."\"><input type='image' src='images/icn_edit.png' title='Edit'></a></td> ";

                                echo "<td><a onclick=\"return confirm('delete this record?')\" href=\"a_del_subcategory.php?id=".$row['id']."\" ><input type='image' src='images/icn_trash.png' title='Trash'></a></td> ";

                            echo "</tr>";
                    }       
                // close table>
        echo "</table>";
        // pagination

echo '<div class="pagination" style="margin-left:300px;" >';
        if ($total_pages > 1) 
            {
                echo paginate($reload, $show_page, $total_pages);
            }
    echo "</ul>
</div>";
?>
</body>


但是问题是我无法正确转换它,并且编辑和删除功能不起作用,即

echo '<td>' . mysql_result($result, $i, 'subcatname') . '</td>';

echo "<td><a href=\"a_edit_subcategory.php?id=".$row['id']."\"><input type='image' src='images/icn_edit.png' title='Edit'></a></td> ";

echo "<td><a onclick=\"return confirm('delete this record?')\" href=\"a_del_subcategory.php?id=".$row['id']."\" ><input type='image' src='images/icn_trash.png' title='Trash'></a></td> ";

最佳答案

尝试这个:

for ($i = $start; $i < $end; $i++) 
{
    if ($i == $total_results) 
        {
            break;
        }

        mysqli_data_seek($result, $i);

        $data = mysqli_fetch_assoc($result);

        // echo out the contents of each row into a table
        echo "<tr " . $cls . ">";
            echo '<td></td>';
            echo '<td>' . $data['subcatname'] . '</td>';
            echo '<td>' . $data['catname'] . '</td>';

            echo "<td><a href=\"a_edit_subcategory.php?id=".$data['id']."\"><input type='image' src='images/icn_edit.png' title='Edit'></a></td> ";

            echo "<td><a onclick=\"return confirm('delete this record?')\" href=\"a_del_subcategory.php?id=".$data['id']."\" ><input type='image' src='images/icn_trash.png' title='Trash'></a></td> ";

        echo "</tr>";
}

关于php - 从sql迁移到mysqli,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29338889/

相关文章:

sql - 用于选择 Redshift Postgres 中多数行名称的窗口函数

php - 使用 Ctrl+Click 选择多个图像并使用 PHP 上传

php - mysql查询失败-给定 bool 值

javascript - 将 PHP 应用程序编译为具有变体的 HTML

php - 如何检查 session 是否已启动并重定向用户?

sql - 用户事件的数据库结构是否正确?

java - VoltDB - 小数值有太多零

java - toString 实现的最佳标准样式是什么?

php - DISTINCT - 再次下载内容

mysql - 嵌套左连接查询不起作用