How to Read "Fetch(Pdo::Fetch_Assoc);"

How to read fetch(PDO::FETCH_ASSOC);

PDOStatement::fetch returns a row from the result set. The parameter PDO::FETCH_ASSOC tells PDO to return the result as an associative array.

The array keys will match your column names. If your table contains columns 'email' and 'password', the array will be structured like:

Array
(
[email] => 'youremail@yourhost.com'
[password] => 'yourpassword'
)

To read data from the 'email' column, do:

$user['email'];

and for 'password':

$user['password'];

Looping through query results with fetch(PDO::FETCH_ASSOC)

As discussed in the comments, I was looking for an alternative to mysqli_num_rows() and found rowCount().

The following now works for me:

$stmt = DB::run("SELECT * FROM admin WHERE username='$manager' AND password='$password' LIMIT 1");
$existCount = $stmt->rowCount();
if ($existCount == 1){

PDO - Fetch Assoc in a 'while' loop

So making the assumption that the only element ever seen is the last element it is because what your are returning is being overwritten each loop. There are a few options to resolve this. The simplest is:

$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();

$text = "";
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$book_id = $row['id'];
$book_title = $row['title'];
$book_image = $row['image'];
$book_amz = $row['amazon'];
$book_desc = $row['description'];
$book_rating = $row['rating'];
$book_date = $row['date'];
$book_author = $row['author'];
$book_categorie = $row['categorie'];

//String concatenation of text will
//give you one big string at the end to return.
$text .= "ID: '{$book_id}'";
}
return $text;

However this will not work well with your real bootstrap html. You need to make sure that the columns add up right.

You will need something a bit more intuitive

Using the actual code it would look something like

$stmt = $this->conn->prepare('SELECT * FROM books');
$stmt->execute();

$bookEcho = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$bookEcho[] = '<div class="col-md-3">
<div class="thumbnail">
<span>' . $book_title . '</span>
<img src="' . $book_image . '">
<div class="book-options">
<span>Bewertung</span><br/>
' . $stars . '
<a href="books.php?id=' . $book_id . '" class="btn btn-read btn-block">Jetzt lesen</a>
</div>
</div>
</div>';
}
return $bookEcho;

Now in your function what ever it is you can do something like (this is not the most elegant thing I have ever written but should get the job done):

$cols = 4;
$colCount = 1;
foreach ($bookEcho as $book){
if($colCount == 0){//create a row}
echo $book;
$coolCount++;
if($colCount == 0){end a row}
if($colCount == 4){ $colCount = 0;}
}

PDO method for mysql_fetch_assoc()?

You can use (PDO::FETCH_ASSOC) constant

Usage will be
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)){
....
}


Here's the reference (documentation precisely) : http://www.php.net/manual/en/pdostatement.fetch.php

Why does PDO fetch() return only the first row?

To select all rows using PDO, you need to use fetchAll() instead of fetch().

$e = $db->query("SELECT `username`, `membership` FROM `users` WHERE `expire` != ''")->fetchAll();

In PDO, fetch() returns "the next row from a result set".

fetchAll() returns "an array containing all of the result set rows".



Related Topics



Leave a reply



Submit