Ahmed Anssaien Ahmed Anssaien - 25 days ago 12
MySQL Question

Is there an equivalent for mysql_result($result, $i, 'ColumnName') in PDO

I can't find the PDO equivalent to mysql_result ($result, $i, 'ColumnName') anywhere. I'm sure I'm doing something wrong, especially at the bottom. I prefer not to use a totally different code. It's been 9 months and today I've decided to change my website's MySQL to PDO, but it's kind of complicated, especially the mysql_result($result, $i...) thingy. Can anyone help?

Here's my code:

$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 />";

Max Max
Answer

Ok, if you really want to reuse this code, you could use

$row = $result->fetch()

After that : echo $row['id']