Executing simple queries
Querying the database is obviously something that we need to do once we are connected to the database. This recipe explains how this can be done, and the different methods available.
Getting ready
To make full of the following recipe, a Zend Framework 2 skeleton application should be used, with a MySQL server available to connect to. Don't forget that connecting to a MySQL server requires the mysql
and mysqli
extension enabled in PHP.
We have configured a database called book
, with the table cards
that has the columns id
, color
, type
, and value
. The SQL query to create the database and table are included in the code that comes with the book.
How to do it…
Queries come in all sort of forms, and in this recipe we will discuss some basic querying.
Using raw SQL
We'll be editing in the /module/Application/src/Application/Controller/IndexController.php
file for this example:
<?php namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; class...