Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Sunday, November 20, 2011

How can you connect to MySQL from PHP?


Ans.  

Create a Connection to a MySQL Database
In PHP, this is done with the mysql_connect() function.
In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// some code
?>

1. How do we declare arrays in PHP?


 Ans

A variable is a storage area holding a number or text. The problem is, a variable will hold only one value.
An array is a special variable, which can store multiple values in one single variable.
An array can hold all your variable values under a single name. And you can access the values by referring to the array name.
Each element in the array has its own index so that it can be easily accessed.
In PHP, there are three kind of arrays:
Numeric array - An array with a numeric index
Associative array - An array where each ID key is associated with a value
Multidimensional array - An array containing one or more arrays

What is $_SESSION variable/array? Why is it used?


Ans.    

A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.

PHP Session Variables
When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.
A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database.
Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is propagated in the URL.

What is $_GET and $_POST variables? Are they built-in or user-defined variables? Why do we use these arrays?


Ans.  

 The predefined $_GET variable is used to collect values in a form with method="get"
Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.
The predefined $_POST variable is used to collect values from a form sent with method="post".
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
All of they are built in variable.
………………………………………………………………………………………..
………………………………………………………………..(Incomplate)

What are built-in and user-defined variables? Give Example.


Ans

PHP provides a large number of predefined variables to all scripts
$GLOBALS
$_SERVER
$_GET
$_POST
$_FILES
$_COOKIE
$_SESSION
$_REQUEST
$_ENV
……………………………………………………………………
……………………………………………….(Incomplate)

How do you define the variable type in PHP?


Ans

In PHP, a variable does not need to be declared before adding a value to it.
PHP automatically converts the variable to the correct data type, depending on its value.
In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
In PHP, the variable is declared automatically when you use it.


Naming Rules for Variables
A variable name must start with a letter or an underscore "_"
A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)

Which symbol is used in PHP to declare variables?


Ans

All variables in PHP start with a $ sign symbol.
The correct way of declaring a variable in PHP:
$var_name = value;

Which symbol is used in PHP to declare variables?


Ans

All variables in PHP start with a $ sign symbol.
The correct way of declaring a variable in PHP:
$var_name = value;

What are the current versions of apache, PHP, and MySQL?


Ans.  
 The current versions of apache is 
Stable Release:
2.2.21 (released 2011-09-13)

The current versions of PHP is
Stable Releases
Current PHP 5.3 Stable: 5.3.8

and The current versions of MySQL is
Stable release 5.5.17