I'm on zend framework 2.
The scenario is, I want to know the actual SQL query after Zend\Db\Sql\Select. I cannot use Zend\Db\Sql\SQL here, and
$select = new Zend\Db\Sql\Select();
$select->where(array($between));
$select->prepareStatement($select)->getSQL();
is giving error like,
Call to a member function getParameterContainer() on null
What is the correct way to write it?
You need to use getSqlString() method.
More info you can find here: https://akrabat.com/displaying-the-generated-sql-from-a-zenddbsql-object/
Related
Hi a Laravel beginner here, I have a a manual select query which retrieve the data correctly via DB::select.
Now I want to paginate the result, however manual pagination doesn't work
$pagination = Paginator::make($book, count($book), 5);
and returning the following error
Call to undefined method Illuminate\Pagination\Paginator::make()
I am using Laravel 4.2
Please help
Thanks
You are using the wrong Paginator class. You should change your import to:
use Illuminate\Support\Facades\Paginator;
Or remove the use statement completely since there's an alias registered for just Paginator
I am currently working on a CMS and am learning how to use OOP.
My question is: How do I take variables set in a mysql database and use them on my website?
Lets say I have 2 columns, one called var_name and var_content.
How would I use it so that $var->sitetitle; echos out whatever it matches up to in my database?
I don't know what this is called, if anyone could lead me in the right direction, I appreciate it!
Right now, I have this:
require_once("classes/database.class.php");
$database = new database();
$database->set_value('sitevariables', $database_prefix . "sitevariables");
$database->set_value('host', $database_host);
$database->set_value('pass', $database_pass);
$database->set_value('user', $database_user);
$database->set_value('table', $database_table);
I wanted it so I could use $database->host to get my mysqli server. It's basically the same thing as I want to do, except it takes from the database to get and set the values
you can define a function to get variables, once your database connection is set up (I sadly not think it is possible to query the database for credentials to connect to that vary database. kind of spiraling thing there.)
this takes into account that var() is a member function of your database() class. Since I don't have your database class, I don't think the way I'll query the database is like yours, but you will be able to fix that.
function var($var_name) {
$stmt = $this->query('SELECT var_content FROM variables WHERE var_name=?');
$stmt->execute(array($var_name));
return $stmt->fetch(PDO::FETCH_COLUMN);
}
$sitetitle = $database->var('sitetitle');
I have some code in Codeigniter that looks like this:
$this->db->select('email_account_id');
$this->db->where('TRIM(LOWER(email_account_incoming_username))',trim(strtolower($email_address)));
$query = $this->db->get($this->users_db.'email_account');
Throws this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND TRIM(LOWER(email_account_incoming_username)) = 'email#server.co.uk'' at line 3
SELECT `email_account_incoming_username`, `email_account_id` FROM (`crmuat_users`.`email_account`) WHERE `email_account_id` IN () AND TRIM(LOWER(email_account_incoming_username)) = 'email#server.co.uk'
Now I don't for the life of me understand how the "IN()" got in there?
Anybody able to see?
You must have called the $this->db->where_in() method somewhere. It is impossible for CodeIgniter Active Record to generate the IN() clause unless you tell it to do so. Try to see if you called the method by accident before you do the select. Also note, that since the Active Record class has this characteristic of singularity in generating query, it is possible that you've called the method somewhere else withing another method that utilizes the Active Record class.
For example, the following will cause the IN() clause to be generated within the final query.
function a()
{
...
$this->db->where_in();
...
$this->b();
}
function b()
{
// Produces the same error as stated in the question because the call of
// where_in() beforehand.
$this->db->select(...)->where(...)->get(...);
}
P.S: Why are you doing trimming and converting the string into lowercase twice? It seems redundant to me.
I am editing somebody else's code and I am a PHP beginner.
I want to access the contents of a column called "email" in the database "tga_purchase_items" where the "id" of the row is "14". I want to save the output to a variable "$sp_email".
The code I have is thus:
$sp_email = $this->db->select("email")->from("tga_purchase_items")->where("id", 14)->get();
The variable is coming out empty although the database field is definitely populated.
What am I doing wrong? I am not used to this "->" syntax at all.
The syntax used in your database leans towards Codeigniter and active record.
If this is the case, the following code will retrieve the email column.
$result_set = $this->db->select("email")->from("tga_purchase_items")->where("id", 14)->get();
$result_object = $result_set->row();
$sp_email = $result_object->email;
What you're missing is retrieving the data from the result set which you get. The get() functions return a result set which is similar to the native MySQLi Result class. This means that you need to retrieve the data you want from this set. There are various functions available in CodeIgniter to do this, namely row(), row_array(), result() and result_array() which you can read about on their manual page.
I haven't done too much with CodeIgniter and the Active Record (I prefer Zend Framework) but I think something like this would do the trick:
$this->db->select('email');
$sp_email = $this->db->get_where('tga_purchase_items', array('id' => '14'));
Look at this for a more detailed explanation: http://codeigniter.com/user_guide/database/active_record.html
I'm creating a behavior that log to a table the sql query executed by a particular Model in a controller. Looking for a method to return me the sql query executed for a particular finder method (like $this->MyModel->find('all') ) I found on the bakery that I can use $this->MyModel->find('sql'), but doesn't work for me. Someone knows how can I achieve this?
Thanks in advance
You can put this function in your app_model.php:
function getLastQueries()
{
$dbo = $this->getDatasource();
$logs = $dbo->_queriesLog;
return $logs;
}
And call it from any model ($this->getLastQueries()) or controller ($this->Model->getLastQueries()) to get them.
$this->Model->find('sql') is not supported natively by Cake. You have to follow the rest of the instructions in the Bakery article for installing a new DBO driver, and adding support for the find('sql') method in your AppModel. Once you do this, it should be able to get you what you're looking for.
http://bakery.cakephp.org/articles/grant_cox/2008/06/23/get-the-find-query-sql-rather-than-query-result