Yii CDbCommand: getText() with parameters not possible? - yii

By executing the following command:
echo Yii::app()->db->createCommand()
->select('*')
->from('User')
->where('user_id=:uid', array(':uid'=>123))
->getText();
we get
SELECT * FROM User WHERE user_id=:uid
when we should be getting
SELECT * FROM User WHERE user_id=123
Binding does not work when using getText(). Why? Is it a bug?

Related

Laravel: toSql function not displaying query correctly

I am trying to diedump the query on my index screen using this line of code:
dd(DB::table('members')->where('name', '=', 'Tycho')->toSql());
Now the problem is that when I am displaying the query on my screen I get this:
"select * from `members` where `name` = ?"
My final goal of these lines of code is that I can save offline queries and execute them when the application is online. Unless someone has a solution for this, I'll have to save the queries in a database.
You are seeing the ? placeholders as Laravel uses Prepared Statements.
See Ijas Ameenudeen's answer on another SO question which details how to add a toRawSql() macro on the Eloquent builder which will replace the placeholders with the bindings that you supplied to the original query.
This is because you are using the toSql method, you can use the getBindings method to get the values / bindings.
oneliner:
$query = DB::table('members')->where('name', '=', 'Tycho')->toSql();
// will give the raw query with bindings.
$sqlWithBindings = str_replace_array('?', $query->getBindings(), $query->toSql());
You can try this:
DB::enableQueryLog();
DB::table('members')->where('name', '=', 'Tycho')->get();
echo "<pre>";
print_r(DB::getQueryLog());

How to use PDO bindParam for SphinxSearch 3?

I have recently installed SphinxSearch 3.1.1 on Ubuntu 18 and am currently creating a snippet of code using PHP 7.2. I am having trouble making bindParam work.
Here is my code:
$mytest = "hello";
$query = $conn->prepare("SELECT *, weight() FROM test WHERE MATCH('\"#title :mytest \"/1')");
$query->bindParam(':mytest', $mytest, PDO::PARAM_STR);
$query->execute();
When I try to execute it, result is empty.
However, when I try to directly put $mytest inside the statement, I get the expected result
$query = $conn->prepare("SELECT *, weight() FROM test WHERE MATCH('\"#title". $mytest ."\"/1')");
Does this mean, sphinx does not support bindParam within the MATCH() function? Or am I missing something else here.
Note from http://php.net/manual/en/pdo.prepare.php
Parameter markers can represent a complete data literal only. Neither part of literal, nor keyword, nor identifier, nor whatever arbitrary query part can be bound using parameters.
ie you trying to bind inside a string literal. In the practical sense PHP (or the mysql server) wll add the ' around the value to make the statement.
... In effect would end up with sphinxql query of:
SELECT *, weight() FROM test WHERE MATCH('"#title 'hellow' "/1')
which is clearly wrong!
Instead bind the whole string literal.
$query = $conn->prepare("SELECT *, weight() FROM test WHERE MATCH(:mytest)");
$query->bindParam(':mytest', '"#title '.$mytest.'"/1', PDO::PARAM_STR);
Although that looks like an invalid Sphinx 'extended syntax' query, The #field qualifer, should be outside the quorum,
$query->bindParam(':mytest', '#title "'.$mytest.'"/1', PDO::PARAM_STR);

JPA native query gives incorrect output

I know this may sound silly but I've been stuck on this problem for too long!
I'm querying a PostgreSQL repository through JPA using native SQL queries. One of my queries looks like this:
#Query(value = "select * from gs where ?1 = ?2", nativeQuery = true)
public List<GsJsonStore> matchJson(String term, String value);
I'm testing the function using :
List<GsJsonStore> list = repo.matchJson("subject", "'Sub'");
The list is empty on running the query, however when I run the same query through PSQL command line using:
select * from gs where subject = 'Sub';
I get the correct output, records contatining the key-value pair are returned.
Where am I making the mistake?
You can't use parameter for column name. Your query resolves to
select * from gs where 'subject' = '''Sub'''
EDIT: just saw #pozs already posted the same in comment

Php mysql statement with set and select

I have a weird problem, when i use the query on phpmyadmin, it works. but when i use using a php script it returns an error.
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in
I tried to troubleshoot and discovered that the problem lies with the set statement.
this is my example code.
$sql = 'set #rank=0; select * from user;';
Please help somebody.
First Run
$sql = set #rank=0;
it will store value of rank
then run:
select * from user;
In sort you need to run both queries separately .
set statement stores values. that can be used by next executing query,
like code below :
$sql ="SET #id:=0";
$Executives=$DB->exec($sql);
$sql = "SELECT #id:=#id+1 as id,pes.* FROM profile_executive_summary as pes where profile_id=".$pid;
$Executives=$DB->fetchAssoc($sql);
See what mysql_error returns after you run mysql_query('...'). That might help. In general, mysql_query only permits one query. You can't separate them by newlines or semicolons. mysqli will do it for you though.

Zend Framework output ready prepared statement

How to output sql statement right before it's launched?
To check all placed data inside prepared statement.
Zend_Db doesn't have a mechanism itself to output the sql statements that it generates. What you can do is modify the public method "query" in Zend/Db/Adapter/Abstract.php(line 445 in 1.10.6) to output the $sql local variable. The query method is called by both the update and insert methods.
There is actually a way to output the SQL it generates
$select = $db->select()->from('elements')
->where('id = ?', $this->_Id);
$sql = $select->__toString();
echo $sql;
You can also use:
echo (string) $select;