When I started learning PHP, I've never touched MySQL_ because everyone told me to start with PDO.
I know about PDO fetch, execute, and more. But I can't really understand how to convert this to PDO:
This is a pagination system, well a little part of it.
// counting the offset
$sql = ("SELECT * FROM comments LIMIT $offset, $rowsPerPage");
$res = mysql_query($sql) or die(mysql_error());
// how many rows we have in database
$sql2 = "SELECT COUNT(comment_id) AS numrows FROM comments";
$res2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_array($res2);
$numrows = $row2['numrows'];
// print the random numbers
while($row = mysql_fetch_array($res))
{
//Echo out your table contents here.
echo $row[1].'<BR>';
echo $row[2].'<BR>';
echo '<BR>';
}
I have no idea what mysql_fetch_array is, well it looks similar to the PDO::FETCH, but I can't really get it..
Can some one give me a few hints on how to convert this MySQL_ to PDO?
thanks.
This is the full code:
My attempt at converting:
// counting the offset
$sql = $pdo->prepare("SELECT * FROM comments LIMIT $offset, $rowsPerPage");
$res = $sql->execute();
// how many rows we have in database
$sql2 = $pdo->prepare("SELECT COUNT(comment_id) AS numrows FROM comments");
$res2 = $sql2->execute();
$row2 = $res2->fetchAll();
$numrows = $row2['numrows'];
// print the random numbers
while($row = $res2->fetchAll())
{
//Echo out your table contents here.
echo $row[1].'<BR>';
echo $row[2].'<BR>';
echo '<BR>';
}
but getting this error:
Fatal error: Call to a member function fetchAll() on a non-object
this line: $row2 = $res2->fetchAll();
what did I do wrong?
I have no idea what mysql_fetch_array is, well it looks similar to the PDO::FETCH
Well, mysql_fetch_array
is a function that will fetch a result (coming from mysql_query
) into an array. PDO::FETCH_*
instead is a family of constants.
Can some one give me a few hints on how to convert this MySQL_ to PDO?
I don't want to seem rude but you should definitely RTM: you can easily find a proper documentation and examples.
Does fetchAll() does the same work as mysql_fetch_array ?
You can take a look at both the documentation for mysql_fetch_array
and PDOStatament::fetchAll
to answer this on your own. Just to make you notice: on the linked documentation page for mysql_fetch_array
it explicitly says:
Alternatives to this function include: [...] PDOStatement::fetch()
I'm getting this error: Fatal error: Call to a member function fetchAll() on a non-object
This:
$res2 = $sql2->execute();
$row2 = $res2->fetchAll();
Is causing that error because execute()
will return a boolean value. To retrieve results via fetchAll()
you should call it on an PDOStatament
(in your case $sql2
). This is the correct code:
$sql2->execute();
$row2 = $sql2->fetchAll();
According to your code, you're using mysql_fetch_array
and fill it with the name of a column in your database:
**$row2** = mysql_fetch_array($res2);
$numrows = **$row2['numrows']**;
You could use PDO::FETCH_ASSOC
so you can continue using the names of column and in addition to that you can use the numeric value of columns (1st column is number 0, 2nd column 1, etc).
I'm not really common with PDO, so please look at answers from the other people aswell. Besides that, I too strongly recommend you to take a look at what different ways you can use to fetch data from your database (_rows, _assoc, _array, etc). There's lots to learn :)