PDO 中是否有 mysql_result($result, $i, 'ColumnName') 的等价物
Is there an equivalent for mysql_result($result, $i, 'ColumnName') in PDO
我在任何地方都找不到等同于 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 />";
好的,如果你真的想重用这段代码,你可以使用
$row = $result->fetch()
之后:echo $row['id']
我在任何地方都找不到等同于 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 />";
好的,如果你真的想重用这段代码,你可以使用
$row = $result->fetch()
之后:echo $row['id']