Friday, 13 February 2015

Which one is the Best Loop Between (For, While, foreach). in PHP

That is always open to debate. Using the System.Diagnostics.Stopwatch class I ran some tests. 100,000 iterations in a for loop that did nothing inside took me 0.0003745 seconds. This was the code for the loop:
for (int i = 0; i < 100000; i++) ;
The while loop resulted 0.0003641 seconds, which is pretty much the same as the for loop. Here is the code
I used:
int i=0;
while (i < 100000)
i++;
The foreach loop has a slightly different purpose. It is meant for itterating through some collection that implements IEnumerable. It's performance is much slower, my test resulted in 0.0009076 seconds with this code:
int[] test = new int[100000];
foreach (int i in test) ;

What is the difference between mysql connect and mysql pconnect in PHP?

The difference between connect() and pconnect, it is simply like a shop when u entering in shop you will open the door and take your iteam come out and close the door that is called connect() in mysql the connection to the mysql database will be automatically closed when the script terminates.
when the door of the shop is already opend and never close it is called pconnect(), open a connection with mysql_pconnect(), the connection will not be closed and will “persist” for future use.

Get all dates between two dates using php code

<?
$strDateFrom = '2009-01-05';
$strDateTo = '2009-01-10';
// takes two dates formatted as YYYY-MM-DD and creates an
// inclusive array of the dates between the from and to dates.
// could test validity of dates here but I'm already doing
// that in the main script
$aryRange=array();
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));
if ($iDateTo>=$iDateFrom)
{
array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
while ($iDateFrom<$iDateTo)
{
$iDateFrom+=86400; // add 24 hours
array_push($aryRange,date('Y-m-d',$iDateFrom));
}
}
echo '<pre>';
print_r($aryRange);
?>

Remove The Password Of A Password Protected PDF Files in UBUNTU

This brief tutorial describes how to remove the password protection from the PDF files. Please note that we are not going to hack or crack the password of PDF file. Instead, we just remove the known password of the PDF file usingqpdf application from Terminal. It is really easy and fun. Let us get started.

What is Qpdf?

From the man pages,
The  qpdf program is used to convert one PDF file to another equivalent PDF file. It is capable of performing a variety of transformations such as linearization (also known as web optimization or fast web viewing), encryption, and decryption of PDF files. It also has many  options for inspecting or checking PDF files, some of which are useful primarily to PDF developers

Install Qpdf

Install Qpdf in Debian based system using command:
sudo apt-get install qpdf
In RPM based systems, we can install it using command:
sudo yum install qpdf

Remove Password Protection from PDF file

I have a password protected PDF file in my home folder, and I know the password. Be mindful that you should have your PDF file password. Otherwise, you can’t remove the password protection using this method.
My password protected PDF file name is password.pdf.
When opening the pdf file, It asks me to enter the password.
password.pdf — Password Required_001
Ofcourse, I know the password ,and I can easily open it using the password. But I really don’t want password protection.
Using the following command, I am going to remove the password from the password.pdf file, and save it with a name nopassword.pdf in the home directory.
qpdf --password='mypassword' --decrypt password.pdf nopassword.pdf
Done!
Then, I opened the PDF file, It didn’t ask to enter the password. Very easy, isn’t it?
For more details about Qpdf, I suggest you to read the Qpdf manual.

PHP & MySQL Transaction mode (Begin , Commit , RollBack)

Table Type : InnoDB
InnoDB Syntax :
  1. CREATE TABLE `customer` (  
  2. .  
  3. .  
  4. .  
  5. .  
  6. ) ENGINE=InnoDB; 

  1. The ‘InnoDB’ feature is disabled; you need MySQL built with ‘InnoDB’ to have it working
    Solution
    my.ini (C:\AppServ\MySQL\my.ini)
    skip-innodb
    Change To
    #skip-innodb
    ### Restart MySQL Service ###
    .
    .
    Example
    php_mysql_transaction1.php
  2. <html>  
  3. <head>  
  4. <title>ShotDev.Com Tutorial</title>  
  5. </head>  
  6. <body>  
  7. <form action="php_mysql_transaction2.php" name="frmAdd" method="post">  
  8. <table width="600" border="1">  
  9. <tr>  
  10. <th width="91"> <div align="center">CustomerID </div></th>  
  11. <th width="160"> <div align="center">Name </div></th>  
  12. <th width="198"> <div align="center">Email </div></th>  
  13. <th width="97"> <div align="center">CountryCode </div></th>  
  14. <th width="70"> <div align="center">Budget </div></th>  
  15. <th width="70"> <div align="center">Used </div></th>  
  16. </tr>  
  17. <tr>  
  18. <td><div align="center"><input type="text" name="txtCustomerID" size="5"></div></td>  
  19. <td><input type="text" name="txtName" size="20"></td>  
  20. <td><input type="text" name="txtEmail" size="20"></td>  
  21. <td><div align="center"><input type="text" name="txtCountryCode" size="2"></div></td>  
  22. <td align="right"><input type="text" name="txtBudget" size="5"></td>  
  23. <td align="right"><input type="text" name="txtUsed" size="5"></td>  
  24. </tr>  
  25. </table>  
  26. <input type="submit" name="submit" value="submit">  
  27. </form>  
  28. </body>  
  29. </html>  
php_mysql_transaction2.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. $objConnect = mysql_connect("localhost","root","root") or die(mysql_error());  
  8. $objDB = mysql_select_db("mydatabase");  
  9.   
  10. //*** Start Transaction ***//  
  11. mysql_query("BEGIN");  
  12.   
  13. //***  Query 1 ***//  
  14. $strSQL = "INSERT INTO customer ";  
  15. $strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";  
  16. $strSQL .="VALUES ";  
  17. $strSQL .="('".$_POST["txtCustomerID"]."','".$_POST["txtName"]."','".$_POST["txtEmail"]."' ";  
  18. $strSQL .=",'".$_POST["txtCountryCode"]."','".$_POST["txtBudget"]."','".$_POST["txtUsed"]."') ";  
  19. $objQuery1 = mysql_query($strSQL);  
  20.   
  21. $strSQL = "INSERT INTO customer ";  
  22. $strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";  
  23. $strSQL .="VALUES ";  
  24. $strSQL .="('".$_POST["txtCustomerID"]."','".$_POST["txtName"]."','".$_POST["txtEmail"]."' ";  
  25. $strSQL .=",'".$_POST["txtCountryCode"]."','".$_POST["txtBudget"]."','".$_POST["txtUsed"]."') ";  
  26. $objQuery2 = mysql_query($strSQL);  
  27.   
  28. if(($objQuery1) and ($objQuery2))  
  29. {  
  30. //*** Commit Transaction ***//  
  31. mysql_query("COMMIT");  
  32. echo "Save Done.";  
  33. }  
  34. else  
  35. {  
  36. //*** RollBack Transaction ***//  
  37. mysql_query("ROLLBACK");  
  38. echo "Error Save [".$strSQL."]";  
  39. }  
  40. mysql_close($objConnect);  
  41. ?>  
  42. </body>  
  43. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP MySQL and Transaction (Begin,Commit,RollBack)
.
.