Friday, June 16, 2006

[PHP - MYSQL] - Connect to database function

1. Create a file dbconnect.php with the code below (Paste this code and change the values in blue as you need)
<?PHP
function exec_query($sql)
{
$server = "server_name";
$username = "user_name";
$pwd = "pwd";
$db_name = "db_name";
$link = mysql_connect($server,$username,$pwd) or die (mysql_error());
mysql_select_db($db_name) or die (mysql_error());
$result=mysql_query($sql) or die (mysql_error());
mysql_close($link);
return $result;
}
?>


2. The code below shows how to use this across files
<?PHP require("dbconnect.php") ?>
<?PHP
$sql = "Select * from tbl_name";
$result = exec_query($sql);
while($row = mysql_fetch_array($result))
{
echo $row[0];
echo $row[1];

}
?>



Bookmark and Share

Tuesday, June 13, 2006

PHP Random String Generator function

function random_gen($length)
{
  $random= "";
  srand((double)microtime()*1000000);
  $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $char_list .= "abcdefghijklmnopqrstuvwxyz";
  $char_list .= "1234567890";
  // Add the special characters to $char_list if needed

  for($i = 0; $i < $length; $i++)  
  {    
     $random .= substr($char_list,(rand()%(strlen($char_list))), 1);  
  }  
  return $random;
}
$random_string = random_gen(10); //This will return a random 10 character string

Share it with others on:
Bookmark and Share

Saturday, June 10, 2006

[GMAIL] - [PHP] - Send email using PHP with Gmail

[ This explains how to use gmail to send emails in php using PHPMailer]

1 Download PHPMailer from http://phpmailer.sourceforge.net
2 Extract to folder phpmailer
3 Create a file email.php
4 Paste this code and change the values in blue as you need (I modified the sample code given on the PHPMailer homepage)

require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username@doamin.com"; //Reply to this email ID
$email="username@domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "
; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

5 Open the file class.smtp.php in phpmailer directory
6 Paste this code
$host = "ssl://smtp.gmail.com";
$port = 465;
before the line 104 #connect to the smtp server
Hint: Search for #connect
7 Open this page in browser and it will send the email using GMail.

Hint: When you want to email the details from a form, set the variables using the form variables.
eg. $mail->Username=$_POST['email']





Bookmark and Share

Tuesday, June 06, 2006

Configuring Apache - PHP - MySQL on Windows (WAMP Configuration)

[Download and Install]
1Download Apache, PHP, MySQL
2Install Apache to its default location and make sure IIS is stopped during installation.
3Extract PHP to C:\PHP folder
4Install MySQL
5Create a folder C:\MyWebsite (This is the folder where you want your website to be)



[Setup php.ini]
1Goto C:\PHP folder and rename php.ini-dist to php.ini and open this file.
2Search for "doc_root =" or goto line# 454 and change this to doc_root = "C:\MyWebsite\"
3Goto Line# 461 and change this to extension_dir = "C:\PHP\ext"
4Goto Line# 598 and change it to extension=php_mysql.dll



[Setup httpd.conf]
1Open C:\Program Files\Apache Group\Apache2\conf\httpd.conf file
2Goto Line# 120, change this to Listen 8080
3Goto Line# 228 and change it to DocumentRoot "C:/MyWebsite"
4Goto Line# 238 and change it to <Directory "C:/php">
5Goto Line# 321 and change it to DirectoryIndex index.html index.html.var index.php index.htm
6Goto Line# 515 and remove the default ScriptAlias paste the following.
ScriptAlias /php/ "c:/php/"
AddType application/x-httpd-php .php .php5
Action application/x-httpd-php "/php/php-cgi.exe"
SetEnv PHPRC "C:/php"


[Testing Your Configuration]
1Goto C:\MyWebsite and create a file index.php
2Open the file and paste this code and save it. <?PHP phpinfo(); ?>
3Open your browser and type http://localhost:8080/
4You should see the complete details of your PHP setup otherwise recheck your configuration.
Bookmark and Share