Magento – Write Sql query

Simple step to execute MySQL query in Magento.
(1) Select SQL :

$objCon = Mage::getSingleton('core/resource')->getConnection('core_write');
$result = $objCon->query('SELECT 'entity_id' FROM 'catalog_product_entity');

if(!$result) {
$rows = $result->fetch(PDO::FETCH_ASSOC);
if(!$rows) {
print_r($rows);
}
}


(2) Insert SQL :

$objCon = Mage::getSingleton('core/resource')->getConnection('core_write');
$objCon->query("insert into table name (fields) values (values) ");

Mage_Cms_Block_Widget_Interface not found

when I was developing Widget of magento in Magento EE version 1.6 . I have spent lot of time behind to fin it out that Mage_Cms_Block_Widget_Interface is not working with Magento EE 1.6 version and got like this "PHP Fatal error: Interface 'Mage_Cms_Block_Widget_Interface' not found."

After I got information about this interface that It moved Mage_Cms_Block_Widget_Interface to Mage_Widget_Block_Interface in Magento EE version 1.6

Magento Admin login problem


Problem:

I had a new installation of magento. But I was unable to login as an administrator. I went to the admin login page, entered correct username and password but was redirected to the same login page. I could not enter the dashboard page. Error message is displayed when I enter wrong username or password. But nothing is displayed and I am redirected to the same login page when I insert correct username and password.

Solution:

I googled and found these solutions:-

1) Use 127.0.0.1 instead of localhost in your url, i.e. using http://127.0.0.1/magento/index.php/admin instead of
http://localhost/magento/index.php/admin . But this didn’t solve my problem.

2) Since I am using Windows XP, I was suggested to open “host” file from
C:\WINDOWS\system32\drivers\etc and have 127.0.0.1 point to something like magento.localhost or even 127.0.0.1 point to http://www.localhost.com . But this also didn’t work either.

3) This solution finally helped me out of this problem. The solution was to modify the core Magento code. Open app/code/core/Mage/Core/Model/Session/Abstract/Varien.php. Comment out the lines 80 to 83. The line number may vary according to the Magento version. But these lines are present somewhere near line 80. You have to comment the comma (,) in line: $this->getCookie()->getPath()//,

// set session cookie params
session_set_cookie_params(
$this->getCookie()->getLifetime(),
$this->getCookie()->getPath()//,
//$this->getCookie()->getDomain(),
//$this->getCookie()->isSecure(),
//$this->getCookie()->getHttponly()
);

Well, I am out of this problem. Hope, this solution you also help you.


Barcodes On your Web apps


The bar code is method of automatic identification and data collection.
Their use is widespread and the technology behind barcodes is constantly improving. Almost every item purchased from a grocery store, department store, and mass merchandiser has a UPC barcode on it. Now rarely will one come across any product that doesn’t have a barcode.

Varieties of barcodes
Barcodes bascially come in two main type: linear (1-dimensional) and Matrix (2-dimensional) with each offering a variety of formats, depending on your application purpose

(1) linear (1-dimensional) : The barcodes we usually see on books and other standard products are of the linear type. These only encode data in one dimension, from left to right. The following shows a typical UPC barcode.
(2) Matrix (2-dimensional) : They also come in patterns of squares, dots, hexagons and other geometric patterns within images termed 2D (2 dimensional) matrix codes or symbologies.

It is to use the PEAR::Image_Barcode class in PHP to dynamically create barcodes for Web applications. The backbone to the PEAR::Image_Barcode's ability to produce barcode images is in the GD library,If you haven't already installed PEAR, you will need to do so in order to get this application to work.


Installation
You can install Image_Barcode issuing the following command (as root):
# pear install Image_Barcode

You can get the latest code at the PEAR site:
http://pear.php.net/package/Image_Barcode/


Using Image_Barcode
Creating a barcode is as simple as calling the classes static draw() method.
The method has four parameters:

Text : string that shall be converted into barcode representation.
Type : this parameter determines which driver/type should be used; it is one of: Code39, code128, ean13, int25, postnet or upca.
Imgtype : determines the type of the image that is generated; one of jpg, png and gif.

By default, the image generated by the barcode driver is directly output to the browser. If you do not want this, pass false as fourth parameter and draw() will return the GD image resource object; allowing you to do further things with it.

Example code
A sample code for generating a CODE128 barcode is shown below.

/* Data that will be encoded in the bar code */
$bar_code_data = "TRSD5656";

/* The third parameter can accept any from the following,
* jpg, png and gif.
*/
Image_Barcode::draw($bar_code_data, 'code128', 'png');
?>

Supported Types
The ‘Image_Barcode’ library supports the following barcode types:
* Code 39
* Code 128
* EAN 13
* INT 25
* PostNet
* UPCA

Changing Apache's www root directory

Sometimes it's necessary to change your current www root folder to something else. Maybe you have all your work files on a different partition than where you installed WAMP to or you have several websites you're working on and need to make one of them your primary site for a while (the one that shows up when entering localhost).

Anything that has to do with Apache configuration can be done within a file called httpd.conf.

1. WAMP provides quick access to certain important files, one of them being httpd.conf.
2. Left-click WAMP icon and select Config files › httpd.conf.
3. The file will probably open up in Notepad.
4. Press Ctrl+F and enter documentroot.
5. Make sure case sensitive search is disabled and hit enter.
6. You should find a line that looks like DocumentRoot "C:/Program Files/wamp/www/".
7. Copy that line then put a # in front of it: #DocumentRoot "C:/Program Files/wamp/www/".
8. Paste the copied line just below and adjust it to where your custom root folder is located (e.g. DocumentRoot "C:/my_cool_site/").
9. Resume the search (usually by pressing F3) and you'll find a line like: .
10. Again copy the line, put a # in front, paste copied line below and adjust it (e.g. ).
11. Save and close the file.

Whenever you make a change to httpd.conf you have to restart Apache for it to take effect. The # we've added in front of the old lines turns them into a comment which Apache ignores. We just added them so that we can easily switch back to an old configuration by uncommenting the lines.

1. Left-click the WAMP icon and select Apache › Restart Service.
2. Wait for the icon status to change to white again.

The root directory should now have changed to then one you've entered in httpd.conf. You can test this by entering localhost in your web browser. The index page in that folder (or the lack thereof) should show up now.

How to get the Next Auto Increment ID in Mysql

Two way are there in mysql

1st way :
SELECT Auto_increment FROM information_schema.tables WHERE table_name='%s';"


2nd way using this sql :

$rs = mysql_query("SHOW TABLE STATUS LIKE 'table_name' ");
$row = mysql_fetch_array($rs);
echo $row['Auto_increment'];

PHP Form Validation By Regular Expression ()

(1) PHP check valid url

function valid_url($str)
{
return ( ! preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $str)) ? FALSE : TRUE;
}

(2) PHP numeric characters validator

function numeric($str)
{
return ( ! ereg("^[0-9\.]+$", $str)) ? FALSE : TRUE;
}

(3) PHP alpha-numeric characters validator

function alpha_numeric($str)
{
return ( ! preg_match("/^([-a-z0-9])+$/i", $str)) ? FALSE : TRUE;
}

(4) PHP alpha characters validator

function alpha($str)
{
return ( ! preg_match("/^([-a-z])+$/i", $str)) ? FALSE : TRUE;
}

(5) PHP validate ip

function valid_ip($ip)
{
return ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $ip)) ? FALSE : TRUE;
}

(6) PHP validate email

function valid_email($str)
{
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}

(7) PHP Windows Filename Validation
function is_valid_filename($name) {
$parts=preg_split("/(\/|".preg_quote("\\").")/",$name);
if (preg_match("/[a-z]:/i",$parts[0])) {
unset($parts[0]);
}
foreach ($parts as $part) {
if (
preg_match("/[".preg_quote("^|?*<\":>","/")."\a\b\c\e\x\v\s]/",$part)||
preg_match("/^(PRN|CON|AUX|CLOCK$|NUL|COMd|LPTd)$/im",str_replace(".","\n",$part))
) {
return false;
}
}
return true;
}


Reference from : http://www.roscripts.com/snippets/show/61