Showing posts with label Interview. Show all posts
Showing posts with label Interview. Show all posts

Friday, 9 December 2016

Swap variable values in php


Swap variable values in php

<?php
$a = 40;
$b = 50;

$temp = $a;
$a = $b;
$b = $temp;

echo $a;
echo $b;

OR

Swap values without third variable

$a = 40;
$b = 50;

$a = $a+$b;
$b = $a - $b;
$a = $a - $b;

echo $a;
echo $b;

?>

Star pattern programs with for loop in php


Star pattern programs with for loop in php

Script for following pattern

*
**
***
****
*****
******
*******
********

<?php
for($i=1;$i<=8;$i++){
echo str_repeat('*',$i);
echo "<br/>";
}

OR

for($i=1;$i<=8;$i++){

   for($j=1;$j<$i;$j++){
echo '*';
   }
   echo '<br/>';
}

?>


Script for following pattern

****
****
****
****
****
****

<?php
for ($i=1;$i<=6;$i++){
for($j=1;$j<5;$j++){
echo '*';
}
echo '<br/>';
}
?>


Script for following pattern

*****
****
***
**
*

<?php
for($i=6;$i>0;$i--){
for($j=1;$j<$i;$j++){
echo '*';
}
echo '<br/>';
}
?>

Script for Triangle of Stars in PHP

        *
       **
      ***
     ****
    *****
   ******

<?php
$l = 1;
for($i=8;$i>1;$i--){

for($j=1;$j<$i;$j++){
echo '&nbsp;';
}


for($k=1;$k<$l;$k++){
echo '*';
}
$l++;
echo '<br/>';

}

OR

$l = 1;
for($i=8;$i>1;$i--){

echo str_repeat('&nbsp;',$i);
echo str_repeat('*',$l++);
echo '<br/>';
}

?>

Friday, 16 October 2015

HAVING in mysql PHP


HAVING in mysql PHP

We can not use where keyword with aggregate functions That's why the MySQL HAVING clause uses with aggregate functions. The MySQL HAVING clause is often used with the GROUP BY clause.

For example :

Wrong Query :

$query = 'select count(order_id),customer_id from sales_order group by customer_id where count(order_id) > 10';

Right Query :

$query = 'select count(order_id),customer_id from sales_order group by customer_id HAVING count(order_id) > 10';

Thursday, 15 October 2015

Foreach statement with MySQL select query in PHP


Foreach statement with MySQL select query in PHP

Here i have given a example for better understand...
<?php

class myclass{

public function fetchCat(){
   
    $sql = "select c.cat_id,cat_name,product_name,price from categories as c left join products as p on p.cat_id=c.cat_id";
$result = $this->query($sql);
$Finarray = array();
while($rows = mysql_fetch_assoc($result)){
$Finarray[] = $rows;
}
return $Finarray;

    }

}

$cat = $obj->fetchCat();

?>

<table border="1">
    <tr>
        <td>Category Id</td>
        <td>Product Name</td>
        <td>Product Price</td>
        <td>Category Name</td>
    </tr>
    <?php foreach($cat as $_cat): ?>
        <tr>
            <td><?php echo $_cat['cat_id']; ?></td>
            <td><?php echo $_cat['product_name']; ?></td>
            <td>
<?php if(!$_cat['price']): ?>
                &nbsp;
                <?php else: ?>  
<?php echo $_cat['price']; ?>
                <?php endif; ?>  
            </td>
            <td><?php echo $_cat['cat_name']; ?></td>
        </tr>  
    <?php endforeach; ?>
</table>

Tuesday, 6 October 2015

How to remove unique key from mysql table


How to remove unique key from mysql table

Use below query :

ALTER TABLE `table_name` DROP INDEX key_name;

If you don't know the key_name then first try below query, you can get key_name.

SHOW CREATE TABLE table_name

OR

SHOW INDEX FROM table_name;

If you want to remove/drop primary key from mysql table, Use below query for that

ALTER TABLE `products` DROP INDEX `PRIMARY`;

Wednesday, 5 November 2014

Difference between echo and print in php


Difference between echo and print in php

Echo is a statement and print is a function
Echo does not returns value where as print returns value
Echo is a faster than print

Wednesday, 29 October 2014

Difference between cookies and sessions PHP


Difference between cookies and sessions PHP

The main difference between cookies and sessions is that cookies are stored in the user's browsers means client machine and sessions are not. This difference determines what each is best used for session value stores on sever.

A cookie can keep information in the user's browsers until deleted. If a person has a login and password, this can be set as a cookie in their browser so the do not have to re-login to your website every time they visit. you can store almost anything in a browser cookie.

The problem with sessions is that when you close your browser you also lose the session. so if you had a site requiring a login this could not be saved as a session like it could as a cookie.

The session will be deleted after the user has left the website.

How to save image in MySQL database ?


How to save image in MySQL database ?

(1) Read the image
(2) Encode the image data.
(3) Make field with Blob data type
(4) Save binary data in DB

Display : Select image and decode the data.

Monday, 23 June 2014

How to fetch randomly two records from table in mysql


How to fetch randomly two records from table in mysql

select * from table order by rand() limit 0,2

Difference between include and require in PHP


Difference between include and require in PHP

If include and require fail, include produces a warning while require produces fatal error.

get_class() in PHP


get_class() in PHP

Returns the name of the class of an object.

get_class($object);

Global variable in PHP


Global variable in PHP

$_COOKIE
$_ENV
$_FILES
$_GET
$_POST
$_REQUEST
$_SESSION
$_SERVER

isset() in PHP


isset() in PHP

isset() determine if a variable is set and is not null.

What is register_global in PHP.ini ?


What is register_global in PHP.ini ?

By default register_global is off. when it on, there is no need to define variable, every variables are set.

Example :

if($test){ echo "Hello"; } // this condition true
$_POST['username'] you can use $username for this.
when it off above condition is not true.

what are the database engine or what are the table types in PHP ?


what are the database engine or what are the table types in PHP ?

MYISAM
InnoDB
MERGE
Memory
Archive
CSV
Faderated
Blackhole

MYISAM :
                    is the default storage engine. The size of MYISAM table can be up to 256TB.

InnoDB:
                    InnoDB tables supports foreign keys, commit, roolback, roll and forward oparations. size can be up to 64TB.

Merge :
                   Virtual Table.

Archive:
                   Storage large number of records but allow only insert and select command.
CSV :
                   store data in comma separated values.
Federated:
                   Manage data from remote mysql server.

Sunday, 22 June 2014

what are the string functions in PHP ?


what are the string functions in PHP ?

strlen()
strstr()
stristr()
substr()
explode()
strpos()
sha1()
md5()

Saturday, 21 June 2014

CURL in PHP


CURL in PHP

Read data from other websites.
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // optional
curl_setopt($ch, CURLOPT_CONNECTIONTIMEOUT,$time); // optional
$data = curl_exec($ch);
curl_close($ch);
echo $data;

what is XML ?


what is XML ?

XML is a format for string text and transporting data. XML stands for extensible Markup Language.

Data read from xml file.
   simplexml_load_file()
   $xml = simplexml_load_file("file.xml"); // absolute path
   you can see the output in array.

Make xml file through code.
  object : new DOMdocument.

Difference between PHP4.0 and PHP5.0 ?


Difference between PHP4.0 and PHP5.0 ?

PHP4 uses zend 1.0 where as PHP5 uses zend 2.0.
PHP5 included additional oops concept than PHP4 like inheritance, _constructor, _autoload.