How to get raw sql query in yii or yii2 framework

If you have used the query builder in yii 2.0.8 framework then to get the raw sql generated use the below statement.

$query->andFilterWhere([‘like’, ‘username’, $this->username])
->andFilterWhere([‘like’, ’email_id’, $this->email])
->andFilterWhere([‘registration_date’ => $this->register_date]);
// This is echo the raw query generated
var_dump($query->createCommand()->getRawSql());

how to get sql query with data binding from facade DB::table in laravel 5

We can get the SQL query from DB sql query in Laravel 5

$dataQuery = DB::table(‘sometable’)
->select(‘sometable.column1′, ‘sometable.column2′, ‘sometable.column3′, ‘sometable.column4′, ‘sometable.column5′);

$dataQuery->toSql(); // this will give you the actual query but this will not give the result with actual data values.

Now What ??

To get the Data binded with the sql statements , you should use the below function

$dataQuery->getBindings(); // This will return an array of data binded to the sql statement.

Now to get the final sql statement, you can use the below function and pass $dataQuery->toSql() and $dataQuery->getBindings() to the functions written below , which you can define it in your helper class or common functions class file.

function getSqlWithBinding($sql,$bindDataArr){
foreach($bindDataArr as $binding)
{
$value = is_numeric($binding) ? $binding : “‘”.$binding.”‘”;
$sql = preg_replace(‘/\?/’, $value, $sql, 1);
}
return $sql;
}

$sqlwithData = getSqlWithBinding($dataQuery->toSql(),$dataQuery->getBindings());
or something like below if you have defined getSqlWithBinding function in common class.

$sql = CommonFunctionClass::getSqlWithBinding($dataQuery->toSql(),$dataQuery->getBindings());

how to get sql query from facade DB::table in laravel 5

Normally we fetch the data as below

$dataresult = DB::table(‘sometable’)
->select(‘sometable.column1’, ‘sometable.column2’, ‘sometable.column3’, ‘sometable.column4’, ‘sometable.column5’)
->get();

Now if we want to get the actaul query from $dataresult using $dataresult->toSql(); // it will not work

The solution for this is first prepare the query without the get() method and then call the toSql() method as below.

$dataQuery = DB::table(‘sometable’)
->select(‘sometable.column1’, ‘sometable.column2’, ‘sometable.column3’, ‘sometable.column4’, ‘sometable.column5’);

$dataQuery->toSql(); // this will give you the actual query

After that you can fetch the result as below.
$dataresult = $dataQuery->get();

Read the next article to know how to get the sql with data binding

How to Change Timezone in CentOS

You check the time from the command line (run date), and find that the timezone is set to UTC or some other timezone. How do you get this changed?
To change this you have to have root access to the server.

There are a series of time zone files located at /usr/share/zoneinfo. Select the appropriate named timezone for your location. For my location I would need , Kolkata, Asia.

Make note of the appropriate folder and file for your timezone. The active timezone used on your system is in the /etc/localtime file.

First, make a backup of the existing localtime file. It’s always safe to make backups of original config files.

sudo mv /etc/localtime /etc/localtime.bak_01012015

Next, create the link:

sudo ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime

To test your change run the date command. You will see the new time.

Timezone issue with cron

If your cron job timing does’t match with the server time zone for example. You have set the cron to run at 12:30 PM, but when you see the cron log the time show say 3:40 PM. So to run your cron job as per your time zone Say if you are in India, your cron job should run as per Indian time. So you have to add the following line before your cron job line.

CRON_TZ=”Asia/Kolkata” ## This is the important line
03 18 * * * wget http://www.abc.com/test.php >> /var/www/html/abc/logs/test-`date +\%Y\%m\%d`.log

`date +\%Y\%m\%d` => This will create the log as per date month and year wise. Date format enclosed by tick (`) sign

validate zipcode pincode on keypress using jquery

How to validate and restrict unwanted character for zipcode or pincode field using jquery

$(document).ready(function () {
$(‘#pincode’).keyup(function () {
var $th = $(this);
$th.val($th.val().replace(/[^0-9]/g, function (str) {
return ”;
}));
});
});

drupal clean URLs not working? with tilde sign

If your site is developed in Drupal and your URL looks like this:

If your site URL looks like this:

http://[domain name OR IP Address]/~[accountname]/anyFolder/

Example
http://example.com/~xyzaccount/somefolder/
http://xxx.xxx.xxx.xxx/~xyzaccount/somefolder/

Then you need to set RewriteBase!in your .htaccess file that looks like below

RewriteBase /~xyzaccount/somefolder

Force HTTPS url with .htaccess

RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don’t put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]