Friday, April 27, 2012

PHP Error Handling via htaccess

We can restrict PHP Errors via htaccess

PHP error handling for production servers

php_flag  log_errors on
php_value error_log  /home/path/public_html/domain/PHP_errors.log

php_flag display_errors off
php_flag html_errors off
 
# prevent access to PHP error log
<Files PHP_errors.log>
 Order allow,deny
 Deny from all
 Satisfy All
</Files> 

# PHP error handling for production servers
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_flag log_errors on
php_flag ignore_repeated_errors off
php_flag ignore_repeated_source off
php_flag report_memleaks on
php_flag track_errors on
php_value docref_root 0
php_value docref_ext 0
php_value error_log /home/path/public_html/domain/PHP_errors.log
# [see footnote 3] # php_value error_reporting 999999999
php_value error_reporting -1
php_value log_errors_max_len 0

<Files PHP_errors.log>
 Order allow,deny
 Deny from all
 Satisfy All
</Files>

Thursday, April 26, 2012

How to fill an array with values specifying keys

array_fill_keysFill an array with values, specifying keys

<?php
$keys 
= array('foo'510'bar');$a array_fill_keys($keys'banana');print_r($a);?>


The above output

Array ( [foo] => banana [5] => banana [10] => banana [bar] => banana )  

Tuesday, April 24, 2012

document.formname.submit() is not a function or not working

When we are trying to submit a form using java script some times it will not working properly.

Find the below case

I have form like this

<form name="myform" method="post">
  <input type="text" name="name" value="">
  <input type="submit" name="submit" value="GO">
</form>

Here i am trying to submit the the form using java script
<script>
      document.myform.submit();
</script>

Error:- document.myform.submit() not a function

I have solution here change form submit name

<input type="submit" name="register" values="Go">

Don't put the form submit name as submit.





Number format in PHP

number_formatFormat a number with grouped thousands

<?php

$number 
1234.56;
// english notation (default)$english_format_number number_format($number);// 1,235

// French notation
$nombre_format_francais number_format($number2','' ');


?> 

Monday, April 23, 2012

How to create index in mysql table feild

We can create the index for table field very easily. Find the below code

ALTER TABLE `product_name` ADD INDEX ( `deal_id` ) ;

Explain advantages of MyISAM over InnoDB?


MyISAM table is stored in a separate files, which could be compressed then with myisamchk if needed. With InnoDB the tables are stored in tablespace, and not much further optimization is possible. TRhe COUNT(*)s execute slower than in MyISAM due to tablespace etc

Explain advantages of InnoDB over MyISAM?

Row-level locking, transactions, foreign key constraints and crash recovery.

How to find number of unique values in table?

Using mysql query we can find the distinct values find the below query for your reffrence


SELECT COUNT (DISTINCT empname) FROM emp;

What are the different tables engines in MySQL

1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM
MyISAM is the default storage engine as of MySQL 3.23 and as a result if
we do not specify the table name explicitly it will be assigned to the
default engine.

In how many ways we can retrieve the data in the result set of MySQL using PHP?

We can fetch the data from the database by 4 ways

1. mysql_fetch_row.
2. mysql_fetch_array
3. mysql_fetch_object
4. mysql_fetch_assoc

What’s the default port for MySQL Server?

The Default port of mysql is: 3306

How to write IN query in mysql?

Suppose if you want to employee names from empl table who is having emp_id is 1,4,6 or 8

Check the below code for solution

SELECT emp_name FROM empWHERE emp_id IN (1, 4, 6, 8)

How do you get the number of rows affected by query?

SELECT COUNT (user_id) FROM users would only return the number of user_id’s.

How delete dublicate records in mysql table?

Find the following code using this code we can delete duplicate records

If you want to delete total dublicate records write check with the below code

delete from emp_sal USING emp_sal, emp_sal as vtable WHERE (NOT emp_sal.id=vtable.id) AND (emp_sal.sal=vtable.sal)

If you want to delete more one dublicate records check the below code

delete from emp_sal USING emp_sal, emp_sal as vtable WHERE emp_sal.id > vtable.id AND (emp_sal.sal=vtable.sal)

Thursday, April 19, 2012

How to add google search results to our website

The following code shows the google search results shows to our website

<!-- SiteSearch Google -->
<form method="get" action="http://home.wangjianshuo.com/archives/20060120_search_this_site.htm" target="_top">
<table border="0" bgcolor="#ffffff">
<tr><td nowrap="nowrap" valign="top" align="left" height="32">
<a href="http://www.google.com/">
<img src="http://www.google.com/logos/Logo_25wht.gif" border="0" alt="Google" align="middle"></img></a>
</td>
<td nowrap="nowrap">
<input type="hidden" name="domains" value="home.wangjianshuo.com"></input>
<input type="text" name="q" size="20" maxlength="255" value=""></input>
<input type="submit" name="sa" value="Search"></input>
</td></tr>
<tr>
<td> </td>
<td nowrap="nowrap">
<table>
<tr>
<td>
<input type="radio" name="sitesearch" value="" checked="checked"></input>
<font size="-1" color="#000000">Web</font>
</td>
<td>
<input type="radio" name="sitesearch" value="home.wangjianshuo.com"></input>
<font size="-1" color="#000000">home.wangjianshuo.com</font>
</td>
</tr>
</table>
<input type="hidden" name="client" value="pub-8513779941474461"></input>
<input type="hidden" name="forid" value="1"></input>
<input type="hidden" name="channel" value="6801625507"></input>
<input type="hidden" name="ie" value="ISO-8859-1"></input>
<input type="hidden" name="oe" value="ISO-8859-1"></input>
<input type="hidden" name="safe" value="active"></input>
<input type="hidden" name="flav" value="0000"></input>
<input type="hidden" name="sig" value="NdyQdGFpJnNH_B3d"></input>
<input type="hidden" name="cof" value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:11"></input>
<input type="hidden" name="hl" value="en"></input>
</td></tr></table>
</form>
<!-- SiteSearch Google -->
Search Result Code
<!-- Google Search Result Snippet Begins -->
<div id="googleSearchUnitIframe"></div>
<script type="text/javascript">
var googleSearchIframeName = 'googleSearchUnitIframe';
var googleSearchFrameWidth = 650;
var googleSearchFrameHeight = 1300;
var googleSearchFrameborder = 0 ;
</script>
<script type="text/javascript"
src="http://www.google.com/afsonline/show_afs_search.js">
</script>
<!-- Google Search Result Snippet Ends -->

Thursday, April 12, 2012

Google Apps OPen ID with PHP

Google supports OpenID authentication or behaves as openid identity provider, using Google Apps accounts.
This is especially useful for companies to unite other internal services with Google Apps single sign-in point. This is related to Standard edition as well.

I have done my requirement with Google Apps OPEN-ID

Please go through with below URLS. You will get the solution

http://a32.me/2010/02/google-apps-premier-federated-login-with-php/ 

Tuesday, April 10, 2012

ereg is deprecated` errors in PHP 5.3+

If your working with PHP 5.3 or heighter, chances are high you’re going to run into a few warnings or deprecated function messages.
  
TO
ereg
('\.([^\.]*$)', string $string [, array &$regs ] );

Change 
preg_match('/\.([^\.]*$)/', $subject$matches);

How to send an error messages one log using PHP?

error_logSend an error message somewhere

error_log("You messed up!"3"/var/tmp/my-errors.log");

 

error_reporting in PHP?

<?php
// Turn off all error reportingerror_reporting(0);
// Report simple running errorserror_reporting(E_ERROR E_WARNING E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR E_WARNING E_PARSE E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL E_NOTICE);
// Report all PHP errors (see changelog)error_reporting(E_ALL);
// Report all PHP errorserror_reporting(-1);
// Same as error_reporting(E_ALL);ini_set('error_reporting'E_ALL);
?>

Sunday, April 8, 2012

Difference between Php4 and Php5

Find the below major changes in PHP4 & PHP5
  • PHP 5, the object model was rewritten to allow for better performance and more features
  • We can define abstract class
  • public,private and protected methods interdused
  • we can pass values by reffrence
  • In PHP5 new array functions are interduced
  • PHP 5 are the inclusions of Visibility, Abstract, Final Classes and method magic methods, interfaces, cloning,typehinting etc
  • New error level named E_STRICT has been introduced.
     
     
     

How to exchange all keys with their associated values in PHP?

array_flipExchanges all keys with their associated values in an array

<?php
$trans
= array("a" => 1, "b" => 1, "c" => 2);$trans = array_flip($trans);print_r($trans);?>
 
o/p:-
 
Array
(
    [1] => b
    [2] => c
)
 

Friday, April 6, 2012

HTML5 - Advantages

Find the below advantages?
  1. No longer we have to rely upon third party plug-in in order to render audio/video
  2.  HTML5 removes the need for JavaScript solutions
  3. HTML5 in mobile devices Advanced web application features are available in all mobile browsers supporting the markup language, using the same standard syntax and displaying the same standard behavior.

HTML5 vision

  1. HTML5 improves interoperability and reduces development costs by making precise rules on how to handle all HTML elements, and how to recover from errors.
  2.  Some of the new features in HTML5 are functions for embedding audio, video, graphics, client-side data storage, and interactive documents. 
  3.  HTML5 also contains new elements like <nav>, <header>, <footer>, <figure>…
  4.  The HTML5 working group includes AOL, Apple, Google, IBM, Microsoft, Mozilla, Nokia, Opera, and many hundreds of other vendors.
Note: HTML5 is not a W3C recommendation yet!

how to reads entire file into string?

file_get_contents();
file_get_contentsReads entire file into a string

<?php
$homepage
= file_get_contents('http://www.example.com/');
echo
$homepage;?> 



How To Create a WordPress Theme

Creating a WordPress Theme HTML Structure
Now we’re starting to get into the real meat of WordPress Theme development: coding the HTML structure.
The HTML Structure for Your WordPress Theme
Let’s take a look at the HTML structure we’ll be using for the body of our WordPress Theme.
<html>
<head>
</head>

<body>
<div id="wrapper" class="hfeed">
    <div id="header">
        <div id="masthead">

            <div id="branding">
            </div><!-- #branding -->

            <div id="access">
            </div><!-- #access -->

        </div><!-- #masthead -->
    </div><!-- #header -->

    <div id="main">
        <div id="container">

            <div id="content">
            </div><!-- #content -->

        </div><!-- #container -->

        <div id="primary" class="widget-area">
        </div><!-- #primary .widget-area -->

        <div id="secondary" class="widget-area">
        </div><!-- #secondary -->
    </div><!-- #main -->

    <div id="footer">
        <div id="colophon">

            <div id="site-info">
            </div><!-- #site-info -->

        </div><!-- #colophon -->
    </div><!-- #footer -->
</div><!-- #wrapper -->
</body>
</html>
   
WordPress Theme Template & Directory Structure
While the most minimal of WordPress Themes really only needs an index.php Template and a style.css file (or just the style file if it’s a Child Theme) most WordPress Themes need something a little more solid.
Our new minimal will include 6 files. Make a folder in wp-content/themes/ for your theme—for this tutorial I’ll be using “your-theme” but it can be whatever you want—and create the following files in that new folder (don’t worry, they’ll be blank until the next few steps).
•    index.php
•    header.php
•    sidebar.php
•    footer.php
•    functions.php
•    style.css
Now let’s open up the last file we created, style.css, in a text editor. The first thing we need to do is add a section at the top of this file bracketed by what are called CSS “comments” (these guys: /* and */). It’s here that we need to put the info that tells WordPress about your theme. Without it, your theme won’t show up in the themes panel.

Sunday, April 1, 2012

How to send a mail using PHP

Using PHP we can send mail very easily
mailSend mail
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] );

<?php
$to
= 'test@example.com';$subject = 'Subject';$message = 'Hai this is test description';$headers = 'From: test1@example.com' . "\r\n" .
'Reply-To: test2@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);?>




What is global refference in php

$GLOBALSReferences all variables available in global scope

<?phpfunction test() {
$foo = "local variable";

echo
'$foo in global scope: ' . $GLOBALS["foo"] . "\n";
echo
'$foo in current scope: ' . $foo . "\n";
}
$foo = "Example content";test();?>


$foo in global scope: Example content
$foo in current scope: local variable  
 

Register Global Varibles In PHP

The following list are the Global Variables.

  1. $_GET
  2. $_POST
  3. $_REQUEST
  4. $_SESSION
  5. $_COOKIE
  6. $_ENV
  7. $_SERVET
  8. $_FILES

Creating modules in drupal7.x

Module  Name

The first step in creating a module is to choose a "short name" for it. This short name will be used in all file and function names in your module, so it must start with a letter, and it must contain only lower-case letters and underscores. For this example, we'll choose "current_posts" as the short name.

Create a folder and a module file

Given that our choice of short name is "current_posts" :
  1. Start the module by creating a folder in your Drupal installation at the path:
    • sites/all/modules/current_posts
  2. Create the PHP file for the module :
    • Save it as current_posts.module in the directory sites/all/modules/current_posts
    • As of Drupal 6.x, sites/all/modules is the preferred place for non-core modules (and sites/all/themes for non-core themes), because this places all site-specific files in the sites directory. This allows you to more easily update the core files and modules without erasing your customizations. Alternatively, if you have a multi-site Drupal installation and this module is for only one specific site, you can put it in sites/your-site-folder/modules.
  3. Add an opening PHP tag to the module :
    • <?php
    • Module files begin with the opening PHP tag. Do not place the CVS ID tag in your module. It is no longer needed with drupal.org's conversion to Git. If the coder module gives you error messages about it, then that module has not yet been updated to drupal.org's Git conventions.
  

File Upload Error Messages Explained

The error code can be found in the error segment of the file array that is created during the file upload by PHP. In other words, the error might be found in $_FILES['file']['error']

UPLOAD_ERR_OK
Value: 0; There is no error, the file uploaded with success.
UPLOAD_ERR_INI_SIZE
Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
UPLOAD_ERR_FORM_SIZE
Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
UPLOAD_ERR_PARTIAL
Value: 3; The uploaded file was only partially uploaded.
UPLOAD_ERR_NO_FILE
Value: 4; No file was uploaded.
UPLOAD_ERR_NO_TMP_DIR
Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
UPLOAD_ERR_CANT_WRITE
Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.
UPLOAD_ERR_EXTENSION
Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo may help. Introduced in PHP 5.2.0.