PDO中是否存在mysql_result($ result,$ i,'ColumnName')的等效项

时间:2016-11-13 15:23:46

标签: php mysql pdo pagination equivalent

我无法在任何地方找到相当于mysql_result($ result,$ i,'ColumnName')的PDO。我确定我做错了什么,特别是在底部。我不想使用完全不同的代码。已经9个月了,今天我决定将我的网站的MySQL改为PDO,但它有点复杂,尤其是mysql_result($ result,$ i ...)。有人可以帮忙吗?

这是我的代码:

$db = new PDO("mysql:host=localhost;dbname=myTable", "root", "");
include ('paginate.php'); //include of paginat page
$per_page = 10;         // number of results to show per page

//before: $result=mysql_query("SELECT * FROM TABLE");
$result=$db->query("SELECT * FROM TABLE");


//before: $total_results=mysql_num_rows($result);
$result1=$db->query("SELECT COUNT(*) FROM TABLE");
$total_results = $result1->fetchColumn();
$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']; //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;
$reload = "home?p=table" . "&tpages=" . $tpages;
echo '<center><div class="pagination"><ul>';
if ($total_pages > 1) {
    echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div></center>";?>
echo '<table border ="1" id="table" class="rwd-table sortable">       <tr>';

    echo '<th>ID</th>';
echo "</tr>";

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

                echo '<tr>';
    //before: echo '<td>' . mysql_result($result,$i,'id') . '</td>';
    echo '<td>' . $result[$i]['id'] . '</td>';
        echo "</tr>";         

         }
         echo "</table><br /><br />";

1 个答案:

答案 0 :(得分:2)

好的,如果您真的想重复使用此代码,可以使用

$row = $result->fetch()

之后:echo $row['id']