Thursday, 12 February 2015

send sms through php using cdac service

Before using the sms service we must first install PHP5_CURL as we are using curl function

Command to install is : sudo apt-get install php5-curl

then after restart the apache server:
              sudo   /etc/init.d/apache2 restart

If the above step not done we will receive a error " CALL TO UNDEFINED FUNCTION CURL_INIT"

and, if it is executed in browser you may receive 500 - server error.

So, before proceeding please install the above step.


if((strlen($this->mobile) > 0) && ($this->sec_year!=null) && ($this->sec_fno!=null)){
$mnum = "0".$this->mobile;
$msecno = $this->sec_fno;
$msecyr = $this->sec_year;
if($this->clo_yn<>'Y'){
$data = array(
  "username" => "Username",
"password" => "Password",
"senderid" =>"Service Name",    
"smsservicetype" =>"singlemsg",  // bulkmsg for bulk messages
"mobileno" =>"$mnum",
// "bulkmobno" => "09999999999,07777777777,08888888888,09879879879",
"content"  => "your inward entry no. $this->inw_no with fileno. $msecno/$msecyr is Moved to   $sname"
);
}else{
$data = array(
  "username" => "Username",      
"password" => "Password",  
"senderid" =>"ServiceName",      
"smsservicetype" =>"singlemsg",
"mobileno" =>"$mnum",      
"content"  => "Your Abids Collectorate Inward no. $this->inw_no with fileno. $msecno/$msecyr is CLOSED"
);
}
   //    return post_to_url("http://msdgweb.mgov.gov.in/esms/sendsmsrequest", $data);
$url="http://msdgweb.mgov.gov.in/esms/sendsmsrequest";

$fields = '';
       foreach($data as $key => $value) {
          $fields .= $key . '=' . $value . '&';
       }
       rtrim($fields, '&');
$post = curl_init();

       curl_setopt($post, CURLOPT_URL, $url);
       curl_setopt($post, CURLOPT_POST, count($data));
       curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
       curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
       // echo "response from website  ";
       $result = curl_exec($post);

       curl_close($post);

} 

Simple way to insert or update mysql tables in YII using php

 $connection1=Yii::app()->db;  // Gets the application connection
     
       $sql1="update inw_trans set out_section='$sname',out_time='$tdt',remarks='$rem' where id=$max";
       $command1=$connection1->createCommand($sql1)->execute();


Like wise the insert statements

$connection=Yii::app()->db;
      $sql="insert into inw_trans(inw_no,sec_code,sub_scode,in_time,status,in_section,ipadd) values ($inwno,$sec_code,$subsec,'$tdt','O','$sname','$ipadd')";
        $command=$connection->createCommand($sql)->execute();

Thursday, 5 February 2015

Get a random List using mysql -- Login used for elections

select * from teammaster where ac_no=$cons and allot_flg='N' and psno is null order by rand() limit 1

The above statement gives a random result from the table teammaster.


Database Connection in PHP Db is mysql -- Fetch or get a single result value or single column value from a table in php


To fetch a single record: 

      $username="dbuser";
$password="dbpwd";
$database="dbname";

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

      $initqry = mysql_query("select empname from emp where empid=10");

$initchk = mysql_result($initqry, 0);   // Get single value as the empid if unique and primary key


  To get a result set  from a table method one :


           $qry="Select * from emp order by empname";
     $res=mysql_query($qry);
      $num=mysql_numrows($res);     /* Gives the number of records fetched

      $i=0;
     while($i<$monum){                       /* Reading each record */

$ename=mysql_result($res,$i,"empname");
$eid=mysql_result($res,$i,"empid");
$edesig=mysql_result($res,$i,"desig");
$esal=mysql_result($res,$i,"salary");

               if(strtoupper($ename)=='JAMES'){
            $uqry="update emp set salary=50000 where empid=$eid";
              mysql_query($uqry);
               }
$i++;
     }

  To get a result set  from a table method TWO :

Example #2 mysql_fetch_array() with MYSQL_NUM
<?php
mysql_connect
("localhost", "mysql_user", "mysql_password") or
    die(
"Could not connect: " . mysql_error());mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");

while (
$row = mysql_fetch_array($result, MYSQL_NUM)) {
    
printf("ID: %s  Name: %s", $row[0], $row[1]); 
}
mysql_free_result($result);?>
Example #3 mysql_fetch_array() with MYSQL_ASSOC
<?php
mysql_connect
("localhost", "mysql_user", "mysql_password") or
    die(
"Could not connect: " . mysql_error());mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");

while (
$row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    
printf("ID: %s  Name: %s", $row["id"], $row["name"]);
}
mysql_free_result($result);?>
Example #4 mysql_fetch_array() with MYSQL_BOTH
<?php
mysql_connect
("localhost", "mysql_user", "mysql_password") or
    die(
"Could not connect: " . mysql_error());mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");

while (
$row = mysql_fetch_array($result, MYSQL_BOTH)) {
    
printf ("ID: %s  Name: %s", $row[0], $row["name"]);
}
mysql_free_result($result);?>

Exception Handling in PHP, YII

If page not found throw a new exception using

if(($_POST['cons']==='') or ($_POST['cons']===null))
throw new CHttpException(404,'The requested page does not exist.');


The above one Used in Controllers actions If the specified parameters are not specified by user the new exception is thrown

Use the statements in try{}  block and then catch the exceptions at the end of try block

try{
-----------------------
----------------
-------------------------
}catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
    Dialog::message('Exception',"Caught exception:$e->getMessage()");
}

Exit the code using die() or exit() functions

These are all identical.  die() is just a straight-up alias to exit(), but even if it isn't, it still acts identically.
When one of these functions is given a string argument, it prints out the string before terminating the process. When it encounters an integer under 255, that integer is considered the return code for the process, which gets passed back to the process which invoked the PHP script. This is particularly useful when writing command line applications (PHP isn't web-only!).
As far as the difference between exit, exit(), and exit(0), there really is none. There is definitely no difference between the first two because exit is technically a language construct, not a function, so it can be called with or without parentheses, just like echo. Returning a code of 0means "this program ran successfully/without errors", and while I don't know what exactly happens when you don't pass an argument, PHP.net says that an argument-less exit indicates success, so I would bet it returns 0, though again PHP.net doesn't show a default for the argument.