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;
Related
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());
I need to use this condition in my select statement:
WHERE YEAR(date) = YEAR(CURDATE())
but if I do, like this:
$query = db_select('table', 't');
$query->fields('t');
$query->condition('YEAR\(date\)', 'YEAR(CURDATE())', '=');
Drupal won't have it (even if I do not escape those parenthesis - it simply ignores them) because I get an error:
Column not found: 1054 Unknown column 'YEARdate' in 'where clause':
How to overcome this error?
Hmm.. just like this, it seems:
$query->where('YEAR(date) = YEAR(CURDATE())');
The where allows for the arbitrary SQL:
The where() method allows for the addition of arbitrary SQL as a conditional fragment. $snippet may contain any legal SQL fragment, and if it has variable content it must be added using a named placeholder. The $args array is an array of placeholders and values that will be substituted into the snippet. It is up to the developer to ensure that the snippet is valid SQL. No database-specific modifications are made to the snippet.
Hm, you could also use db_query, it allow you to write SQL queries "without Drupal".
I mean, you'll be able to add custom WHERE statements or any SQL-proper functions, like custom functions ;)
Eg.
$result = db_query('SELECT title FROM {node} WHERE type = "%s" AND title LIKE "%%%s%%"', 'type', 'title');
Use addExpression method :
https://api.drupal.org/api/drupal/includes!database!select.inc/function/SelectQuery%3A%3AaddExpression/7.x
$query = db_select('table', 't');
$query->fields('t');
$query->addExpression('YEAR(t.date) = YEAR(CURDATE())');
$result = $query->execute()->fetchAll();
var_dump($result);
I have a problem with powershell and oracle.
This is my code
Add-Type -Path "C:\app\aasif\product\11.2.0\client_2\odp.net\managed\common\Oracle.ManagedDataAccess.dll"
$con = New-Object Oracle.ManagedDataAccess.Client.OracleConnection("User Id=sys;Password=password;Data Source=myserver/oracle;DBA privilege=SYSDBA")
$con.Open()
$cmd=$con.CreateCommand()
$cmd.CommandText="select distinct owner from all_tables where table_name = 'mytable'"
$rdr=$cmd.ExecuteReader()
if ($rdr.Read()) {
$rdr.GetString(0)
}
$con.Close()
When i execute this query directly with SQLPlus, i have :
RS123
RS456
RS789
RS741
RS963
With my powershell, i can't view all the data returned by the query, but only the first line.
RS123
How can i do this?
Thanks
I think in place of if($rdr.Read()) you have to write while($rdr.Read()) to read and work with all the value from the output of the query.
Actually I am very new in powershell but in general language we have to put this condition in loop.
CreateCommand() returns an OracleCommand. ExecuteReader() returns and OracleDataReader.
Here's and example of using the OracleDataReader class:
$OracleDataReader = $cmd.ExecuteReader()
while ($OracleDataReader.Read()) {
$OracleDataReader.GetString(0)
}
Notice I used GetString. You'll need to use the data reader method appropriate for the data type of the column corresponding to your SQL query. The available methods are listed here. If you don't know what the data type is you can use GetFieldType() where the parameter is the column ID you want to check.
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.
I would like to have access to the Seconds_Behind_Master field, (as returned by SHOW SLAVE STATUS) from inside a stored procedure.
I can't figure out how to get its value inside a variable. None of the usual SET/SELECT syntax seems to work.
Is there a way to do that?
Just for the record: it has turned out to be possible to open a cursor for SHOW statements. This allows to parse the output and work with it inside a stored procedure.
From what I see in the recent docs and MySQL Bug#37187 there does not seem to be any way but SHOW SLAVE STATUS to get to this information.
PHP can grab all the fields of the show slave status into a hashmap, like this:
$db = new pdo(
"mysql:host=your_database_hostname;dbname=your_database_name",
'your_username', 'your_password');
$sql = 'show slave status';
$query = $db->query($sql);
$res = $query->fetchall();
foreach($res as $item){
print ">" . $item["Seconds_Behind_Master"];
}
Which prints 0 seconds because everything is up to date:
>0
I tried for an hour to create a stored procedure to do this. I recommend you don't waste your time.