Monday, November 2, 2009

Keep in mind when using Left Join

The WHERE clause would be false in the following query if tableB.column1 were NULL:

SELECT * FROM tableA LEFT JOIN tableB ON (column1) WHERE tableB.column2=5;

Therefore, it is safe to convert the query to a normal join:

SELECT * FROM tableA, tableB WHERE tableB.column2=5 AND tableA.column1=tableB.column1;

Friday, October 30, 2009

Group by in custom query in drupal

We should Keep in mind the followings when we use group by in custom queries.

ex: SELECT pname, MIN(price) FROM products GROUP BY pname;

1. The column that you GROUP BY must also be in your SELECT statement.
2. Remember to group by the column you want information about and not the one you are applying the aggregate function on. In our above example we wanted information on the pname and the aggregate function was applied to the price column.

More Mysql Indexes affect performance on heavy traffic site

I want to share about index on table columns. Indexes are something extra that you can enable on your MySQL tables to increase performance, but they do have some downsides. When you create a new index MySQL builds a separate block of information that needs to be updated every time there are changes made to the table. This means that if you are constantly updating, inserting and removing entries in your table this could have a negative impact on performance. So avoid using index unnecessarily. That will affect the performance.

Thursday, August 20, 2009

Check the user submitting date

checkdate() is a function to check the date is valid or not.

eg:
var_dump(checkdate(12, 31, 2000)); // return true
var_dump(checkdate(2, 29, 2001)); // return false

Tuesday, June 2, 2009

Drupal watchdog query into file

To avoid record growing in watchdog table I am doing the followings. outfile in mysql is used to write output in a file.
I am using following code to write watchdog output into file.

<?php
function customfunction() {
$datetime = date("Y_m_d_Hi");
$path = "c:/";
db_query("SELECT wid,type,uid,message,severity,link,location,referer,hostname,timestamp FROM watchdog ORDER BY wid DESC LIMIT 0,10 INTO OUTFILE '".$path."watchdog".$datetime.".csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' ");
}
?>

Friday, May 29, 2009

Implode in Javascript - Method 1

toString() method returns one string that contains all the elements in the array separated with commas.
The toString() method is used to convert an array to a string when the array is used in string context.
In this method we cannot modify the seperator. To change seperator go to join().
The following example shows JavaScript to Use an Array's toString() Method.

<html>
<script language="JavaScript">
colors = new Array("Blue","Green","Red");
document.write(colors.toString());
</script>
</html>
Output is Blue,Green,Red

Implode in Javascript - Method 2

To make implode in javascript like php use join method.The join() method converts all the elements to strings and then concatenates all the strings into a longer string.

If an argument is provided in the parameter list, it is used to separate the elements in the string returned by the method.
Ex:
<html>
<script language="JavaScript">
fruit = new Array("1","2","3");
String = fruit.join("-");
document.write("The fruit array contains: ",String);
</script>
</html>

Output : 1-2-3