【问题标题】:moving to mysqli from sql从 sql 迁移到 mysqli
【发布时间】:2015-06-03 00:52:22
【问题描述】:

我有一个代码可以在表格中显示数据以及分页,但代码在 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> ";

【问题讨论】:

    标签: php mysql sql mysqli pagination


    【解决方案1】:

    试试这个:

    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>";
    }
    

    【讨论】:

    • 数据正在显示,但编辑和删除功能仍然无法使用
    • 阅读我答案的最后一行。
    猜你喜欢
    • 1970-01-01
    • 2012-11-26
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多