Hi i am new to ZF2 and not familiar with some of the changes in ZF2. I would like to know how can i execute SQL query directly from Controller.
I have the following code:
public function indexAction()
{
$db = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$sql = "SELECT * FROM books";
$statement = $db->query($sql);
$res = $statement->execute();
if($res instanceof ResultInterface && $res->isQueryResult()){
$resultSet = new ResultSet;
$resultSet->initialize($res);
foreach($resultSet as $row){
echo $row->title . PHP_EOL;
}
}
exit;
/*
return new ViewModel(array(
'books' => $this->getBooksTable()->fetchAll(),
));
*/
}
When the controller above is opened in web browser, it does not show anything. If i echo "Blahh.." before the if statement, it displays the "Blahh.." text correctly.
Does anyone know why it does not display the query result properly? Thx.
try to add this in the top of your controller :
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\ResultSet;
Related
Iam currently trying to use my data in my database and insert it into a formular, which has multiple textfields. The formular is in a different php file and when I try to insert the data, nothing happens. Even if I have a blanked website and try to print it out via echo, none of the data inside my database is shown up.
The first file is where I get all my connections and and work with the database (config.php)
public function article_details($id){
global $connection;
$stmt = $connection->prepare("SELECT * FROM `items` WHERE item_name=:id");
$stmt->bindparam(':id', $id);
$stmt->execute();
}
this file is for the redirect when I click on the a tag and the magic happens (view.php)
<?php
$id=$_GET['id'];
include('config.php');
#header("Location: Website.php?page=add_article");
$call = new article();
$call->article_details($id);
foreach ($call as $row)
echo $row['item_name']; ?>
Since its a bigger project with multiple files, I dont want to spamm them all in here. But if you need more information let me know.
You need to return stmt object then call fetch function on it to display data
public function article_details($id){
global $connection;
$stmt = $connection->prepare("SELECT * FROM `items` WHERE item_name=:id");
$stmt->bindparam(':id', $id);
$stmt->execute();
return $stmt;
}
$call = new article();
$stmt = $call->article_details($id);
while ($row = $stmt->fetch()) {
echo $row['item_name']."<br />\n";
}
Your article_details function is not returning anything so you are basically passing null to the foreach loop. I think this will help.
class article {
//...
public function article_details($id){
global $connection;
$stmt = $connection->prepare("SELECT * FROM `items` WHERE item_name=:id");
$stmt->bindparam(':id', $id);
$stmt->execute();
return $stmt;
}
}
Then ...
$id=$_GET['id'];
include('config.php');
//header("Location: Website.php?page=add_article");
$myarticle = new article();
$call = $myarticle->article_details($id);
foreach ($call as $row){
echo $row['item_name'];
}
Hope that helps.
I'm having a problem with some PHP / MySQL code.
I need a view called gameview for a Star Wars game I'm writing.
If I created the view in MySQL then the code runs perfectly. However, I need this view to be dropped every time the game starts. So if I start without the view "gameview" present in the DB, the page cannot be displayed due to the view not existing. However, the moment I manually add the view into MySQL, it works. I can't see why.
Class code
<?php
class gameView
{
protected $Conn;
public function __construct($Conn)
{
$this->Conn = $Conn;
}
public function dropGameView()
{
$drop = "DROP VIEW if EXISTS gameview;";
$stmt = $this->Conn->prepare($drop);
$stmt->execute(array());
}
public function createGameView()
{
$view = "CREATE VIEW gameview AS SELECT id, name, image, quote FROM person;";
$stmt = $this->Conn->prepare($view);
$stmt->execute(array());
}
public function useGameView()
{
$query = "SELECT * from gameview";
$stmt = $this->Conn->prepare($query);
$stmt->execute(array());
$gameView = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $gameView;
}
}
?>
PHP code
<?php
$gameView = new gameView($Conn);
$finalCharacter = $gameView->useGameView();
$smarty->assign('game_view', $finalCharacter);
?>
Well.... stone the crows. I thought this would be too simple to work, but it did!
<?php
$gameView = new gameView($Conn);
$dropGameView = $gameView->dropGameView();
$smarty->assign('drop_gameview', $dropGameView);
$createGameView = $gameView->createGameView();
$smarty->assign('create_gameview', $createGameView);
$finalCharacter = $gameView->useGameView();
$smarty->assign('game_view', $finalCharacter);
?>
Now to crack on and use the view.
Hello i have a question about the zend framework. I have a database with a column having values yes and no. I wish to be able to retrieve from that column only yes values.
This is my code but no success:
public function getListi(array $filterBy = array())
{
$id = 'yes';
$select = $this->sql->select();
$select->from(self::TABLE);
$select->columns(['security_maintenance']);
$select->where(array(
'security_maintenance' => $id
));
$statement = $this->sql->prepareStatementForSqlObject($select);
return $statement->execute();
}
view :
<div class="value-self"><?php
foreach ($domainii as $row) {
echo $row['security_maintenance'];
}
?></div>
controller:
public function indexAction()
{
return new ViewModel(array(
'domainii' => $this->getDomainModel()->getListi(),
));
}
The column is called security_maintenance. the above does not work but when i query by id it works but that does not help me as querying by id will not help me get yes values from the security_maintenance column.
Hello i got it working after doing some research into some zend documentation. This is my code:
public function getListii(array $filterBy = array())
{
$select = $this->sql->select();
$select->from(self::TABLE);
$select->columns(['security_maintenance']);
$select->where->like('security_maintenance', 'yes%');
$statement = $this->sql->prepareStatementForSqlObject($select);
return $statement->execute();
}
You need to specify the columns you want to select.
try adding the below code
$select->columns(['security_maintenance']);
Currently, I am working on a project using Zend framework 3. In this I have to create dynamic navigation means getting navigation menu(data) from the database. I have already created the static navigation as described in
https://docs.zendframework.com/tutorials/navigation/#setting-up-zend-navigation
but not able created this dynamically.
You could have the controller read the values from your database and then pass them on to the viewmodel. Something like this:
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\ResultSet;
function indexAction() {
$statement = $driver->createStatement('SELECT menu FROM database');
$statement->prepare();
$result = $statement->execute($parameters);
if ($result instanceof ResultInterface && $result->isQueryResult()) {
$resultSet = new ResultSet;
$resultSet->initialize($result);
}
return new ViewModel(array('entries' => $resultSet));
}
Then you can get at the entries in your view .phtml
<?php foreach($this->entries as $entry) {
echo $entry . PHP_EOL;
}?>
class Application_Model_DbTable_Email extends Zend_Db_Table_Abstract
{
protected $_name = 'memberdetail';
function getUserid($email)
{
$subquery = $this->select()
->from('memberdetail', array('memberid'))
->where('email = ?', $email);
$select = $this->select()
->from('usertable', array('userid'))
->join('memberdetail', 'usertable.userid = memberdetail.memberid')
->where('usertable.userid = ?', $subquery);
$row = $select->query()->fetch();
if (!$row) {
echo "User id not found";
} else {
return $userid = $row['userid'];
}
}
}
Hi, I am trying to return the userid from the above queries. However, the queries does not seemed to be executed as I always get refreshed whenever I call this function.
P.S this set of queries were given to me by another member.
it looks like this is being over thought. According to the info provided usertable.userid = memberdetail.memberid with this being the case your function is simple.
/** this function assumes one and only one email will match a memberid
* this function can be improved by validating $email as existing in DB
* prior to querying DB, should be done at form level but could be accomplished here
* with Zend_Validate_Db_RecordExists()
*/
public function getUserIdFromEmail($email) {
$select = $this->select();
$select->where('email = ?',$email);
$row = $this->fetchRow($select);//fetch a single row
if (!is_null($row) {//fetchRow returns null if no row matched
return $row->memeberid;//return memberid as string/integer = usertable.userid
} else {
//handle error
}
}
It would have been useful to tell people you are using Zend framework.
You need to establish a connection to the database for $this as described in steps 1 and 2 in this link:
http://framework.zend.com/manual/en/zend.db.select.html/
You can try this, if it helps:
function getUserid($email){
$select = $this->select()
->setIntegrityCheck(false)
->from(array('m' => 'memberdetail'), array('b.userid'))
->join(array('b' => 'usertable'), 'b.userid = m.memberid')
->where('m.email = ?', $email);
$row = $this->getAdapter()->fetchAll($select);
if (!$row) {
throw new Exception("User id not found");
} else {
return $row->toArray();
}
}