I want use but not work please help me.
$sql = Yii::$app->db("INSERT INTO posts(name) VALUES(:name)");
$sql->bindValues([':name' => 'John']);
$sql->execute();
try create command exceute
Yii::$app->db->createCommand('INSERT INTO posts(name) VALUES(:name)')
->bindValues([':name' => 'John'])->execute();
Related
suppose i have a query in cakephp 3
$post = $this->Posts->get($id, [
'contain' => ['Postmeta']
]);
I want to print it like plain mysql query for example
SELECT * FROM posts....
can anyone please explain this how can i achieve this. Please answer it only in cakephp 3 environment. Is it possible to print it in controller as normal mysql query ? please do not mention queries.log file solution. beacause it is time taking to open file and see the query after every executed query in queries.log file in that is look in cakephp style
Thanks
In Controller, We need to write two lines after query code as follows
$post = $this->Posts->get($id, [
'contain' => ['Postmeta']
]);
echo "<pre>";
print_r(debug($post));die;
It will show all result along with sql query syntax.
Here we are using debug for show result along with sql query.
Query:
$post = $this->Posts->get($id, [
'contain' => ['Postmeta']
]);
For Print/Get SQL Statement of above query you can use debug() function as follow in controller:
$post = $this->Posts->get($id, [
'contain' => ['Postmeta']
]);
debug($post);
Just write:
die(print_r($post));
I am using Yii2 advance template. I have to insert 1000 to 2000 records in MySql Database.
Is it possible to make Multiple UPSERT Query in Yii2.
Please help me with your suggestion/answers. Thank you.
Since version 2.0.14 you have upsert() available.
Your code could look something like this:
$insertValues = [
'timestamp' => gmdate('YmdH'),
'entry_id' => $this->id,
'view_count' => 1,
];
$updateValues = ['view_count' => new \yii\db\Expression('table_name.view_count + 1')];
Yii::$app->db->createCommand()->upsert('table_name', $insertValues, $updateValues)->execute();
You can find the full documentation here: https://www.yiiframework.com/doc/api/2.0/yii-db-command#upsert()-detail
Try with modified batchInsert() method:
$db = \Yii::$app->db;
$sql = $db->queryBuilder->batchInsert($table, $fields, $rows);
$db->createCommand($sql . ' ON DUPLICATE KEY UPDATE')->execute();
I have the following code, but it doesn't work. I think it might be because the method was removed? Not sure what the new way to do it is though. I'm on wordpress.
<php?
mysql_query ("UPDATE $wpdb->users
SET access_key = $newAccessKey
WHERE ID = $currentUserID");
?>
That don't work.
users is the table nome.
Advice??
This script is to be run on a page on php.
First you start with wrong syntax to start PHP script '
Now you should check the type of access_key if it is integer than you wrote right and if it is varchar or text then you should write with ''. For this your query is below.
mysql_query ("UPDATE $wpdb->users
SET access_key = '$newAccessKey'
WHERE ID = $currentUserID");
I hope you will get solution.
use single quote for string vars and be sure for sanitize $wpdb->users use concat
mysql_query ("UPDATE " . $wpdb->users .
" SET access_key = '$newAccessKey'
WHERE ID = $currentUserID");
The reason why it doesn't work for you can be a number of things. You can find it out by enabling error reporting (ini_set('display_errors', true); error_level(E_ALL);) and checking for mysql errors (echo mysql_error();).
Second, if it wasn't a typo when you were writing the question:
<php? doesn't start PHP code. It should be <?php, the way you wrote it it is ignored both by the server and the browser because it is an illegal tag and the code inbetween isn't executed at all.
Apart from that, $wpdb brings its own update() function that you can use:
$wpdb->update(
$wpdb->users,
array( 'access_key' => $newAccessKey ),
array( 'ID' => $currentUserID ),
array(
'%s' // if $newAccessKey is a string
// '%d' // if $newAccessKey is an integer
),
array( '%d' )
);
That way you don't have to worry about the database connection or deprecated functions.
I have make one query using conditional statement.
$query = Student::find()
->where('status=1');
if(isset($params['standard_id']) AND !empty($params['standard_id'])){
$query->andWhere(["standard_id"=>$params['standard_id']]);
}
if(isset($params['section_id']) AND !empty($params['section_id'])){
$query->andWhere(["section_id"=>$params['section_id']]);
}
if(isset($params['year']) AND !empty($params['year'])){
$query->andWhere(["year"=>$params['year']]);
}
//$result = $query->all();
return $query;
Mysql
select *from student where satus=1 and standard_id=3 and section_id=1 and year=2015
If possible to make using where - andWhere in yii2?
please help me
Try this.can't understand your question clearly. may be this will help you.
$model = User::find()
->where('satus> :satus', [':satus' => '1'])
->andWhere('standard_id= :standard_id', [':standard_id' => 3])
->andWhere('section_id= :section_id', [':section_id' => 1])
->andWhere('year= :year', [':year' => 2015])
->all();
Yes, you can use both where() and andWhere(). This should helps you to understand how it works.
https://github.com/yiisoft/yii2/blob/master/framework/db/QueryTrait.php#L101
I need to findout the normal sql query for the below. Can you guys please suggest me how can i achieve in Symfony.
Example:
$r = Doctrine_Query::create()
->select('u.worked_hours')
->from('tasksComments u')
->where('u.tasks_id = ?', $arr_values['tasks_id'])
->andwhere('u.id != ?', $arr_values['id'])
->andwhere('u.created_at LIKE ?', $date."%");
$results1 = $r->execute();
On the query object, use the method getSQL.
In your case:
$r = Doctrine_Query::create()
->select('u.worked_hours')
->from('tasksComments u')
->where('u.tasks_id = ?', $arr_values['tasks_id'])
->andwhere('u.id != ?', $arr_values['id'])
->andwhere('u.created_at LIKE ?', $date."%");
var_dump($r->getSQL()); // print the SQL query - you will need to replace the parameters in the query
var_dump($r->getParams()); // print the parameters of the query so you can easily replace the missing parameters in the query
Note that I don't know the namespace of Doctrine_Query but I am assuming that your Query object in this Query object in the Doctrine API documentation.
Using Symfony 1.4 with Doctrine 1.2.3
echo $q->getSqlQuery();
You can view all the executed queries in the profiler toolbar by clicking on the db panel icon (the last section of the toolbar) then on the [Display runnable query] link under your query.