Jump to content

problem with SQL statement using ZEND FRAMEWORK


simonmcm

Recommended Posts

Hi

 

I am using the ZEND FRAMEWORK

 

and for some reason the results from this SQL statement aren't being displayed in the table:

 

The query in the database model is as follows:

 

 

 

public function getUsersBooks($userName) {


$select = $this->select()

->from('books', array('title', 'author'))

->where('username = ?' ,$userName);

$result = $this->getAdapter()->fetchAll($select);

return $result;

The controller is as follows:

 

 
public function indexAction()
{
$this->authoriseUser();




$session = new Zend_Session_Namespace('MyNamespace');
$userName = $session->username;
$books = new Model_DbTable_Books();
$this->view->books = $books->getUsersBooks($userName);


}

 

And the view is as follows:

 

 

 

<table>
<tr>
<th>Title</th>
<th>Author</th>


</tr>

<tr>
<?php foreach($this->books as $book) : ?>
<td><?php echo $this->escape($book->title);?></td>
<td><?php echo $this->escape($book->author);?></td>
<?php endforeach; ?>
</tr>


</table>

if any one has any ideas why the results of the statement arnt being displayed it will be much apriciated

Link to comment
Share on other sites

if any one has any ideas why the results of the statement arnt being displayed it will be much apriciated

 

The $username does not exist in the database or was not present in the session. Please, before asking questions make sure your data is what you expect it to be.

Link to comment
Share on other sites

I have checked that, the problem is the SQL statement isn't returning the output as an array, whats the syntax to output the result as an array?????

 

fetchAll() returns a Zend_Db_Table_Rowset_Abstract object which is what the syntax within your view is expecting, not an array like you keep talking about. As suggested by Ignace, I'm not sure your query is returning any results. You need to check your results before proceeding to use them.

 

I'm not familiar with the syntax, so forgive me for asking.

 

What does this line do? Specifically, what's the array for?

->from('books', array('title', 'author'))

 

It selects the fields title & author from the books table.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.