How To Connect Database in PHP

How To Connect Database in PHP

<?php
$host=”localhost”;
$user=”root”;
$pass=””;
$database=”databasename”;

$conn=mysql_connect($host,$user,$pass);
mysql_select_db($database);

$_GET = array_map(‘trim’, $_GET);
$_COOKIE = array_map(‘trim’, $_COOKIE);
if(get_magic_quotes_gpc()):
$_GET = array_map(‘stripslashes’, $_GET);
$_POST = array_map(‘stripslashes’, $_POST);
$_COOKIE = array_map(‘stripslashes’, $_COOKIE);
$_REQUEST = array_map(‘stripslashes’, $_REQUEST);
endif;

//$sql = ‘SELECT * FROM create_user’;
//
//$result=mysql_query($sql,$conn);
//
//    while($row = mysql_fetch_assoc($result)) {
//
//       echo $row[‘_company’];
//      }
?>

PHP Interview Question & Answer2

PHP Interview Quesition & Answer2

16.What is the difference between Notify URL and Return URL?

Notify URL and Return URL is used in Paypal Payment Gateway integration. Notify URL is used by PayPal to post information about the transaction. Return URL is sued by the browser; A url where the user needs to be redirected on completion of the payment process.

17.Describe functions STRSTR() and STRISTR.

Both the functions are used to find the first occurrence of a string. Parameters includes: input String, string whose occurrence needs to be found, TRUE or FALSE (If TRUE, the functions return the string before the first occurrence.
STRISTR is similar to STRSTR. However, it is case-insensitive
E.g. strstr ($input_string, string)

18.What are the different types of errors in PHP?

  • E_ERROR: A fatal error that causes script termination
  • E_WARNING: Run-time warning that does not cause script termination
  • E_PARSE: Compile time parse error.
  • E_NOTICE: Run time notice caused due to error in code
  • E_CORE_ERROR: Fatal errors that occur during PHP’s initial startup (installation)
  • E_CORE_WARNING: Warnings that occur during PHP’s initial startup
  • E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.
  • E_USER_ERROR: User-generated error message.
  • E_USER_WARNING: User-generated warning message.
  • E_USER_NOTICE: User-generated notice message.
  • .E_STRICT: Run-time notices.
  • E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error
  • E_ALL: Catches all errors and warnings

19.What are the different types of Errors in PHP?

There are three basic types of runtime errors in PHP:

1. Notices: These are small, non-critical errors that PHP encounters while executing a script – for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all – although the default behavior can be changed.

2. Warnings: Warnings are more severe errors like attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.

3. Fatal errors: These are critical errors – for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP’s default behavior is to display them to the user when they take place.

20.How can we know the number of days between two given dates using PHP?

The start date and end date can be first found as shown below:

$date1= strotime($start_date);
$date2= strotime($end_date);
$date_diff = (($date1)- ($date2)) / (60*60*24)

21.How to connect PHP wil MySQL?

<?
mysql_connect(‘db.domain.com:33306′,’root’,’user’);
mysql_connect(‘localhost:/tmp/mysql.sock’);
mysql_connect(‘localhost’,’rasmus’,’foobar’,
true,MYSQL_CLIENT_SSL|MYSQL_CLIENT_COMPRESS);
?>

22.HTTP headers can be used in PHP by redirection which is written as:

<?header(‘Location: http://www.php.net’)?>
The headers can be added to HTTP response in PHP using the header(). The response headers are sent before any actual response being sent. The HTTP headers have to be sent before taking the output of any data. The statement above gets included at the top of the script.

23.Why PHP is also called as Scripting language?

PHP is basically a general purpose language, which is used to write scripts. Scripts are normal computer files that consist of instructions written in PHP language. It tells the computer to execute the file and print the output on the screen. PHP is used for webpages and to create websites, thus included as scripting language.

24.Why many companies are switching their current business language to PHP? Where PHP basically used?

PHP is rapidly gaining the popularity and many companies are switching their current language for this language. PHP is a server side scripting language. PHP executes the instructions on the server itself. Server is a computer where the web site is located. PHP is used to create dynamic pages and provides faster execution of the instructions.

25.What is the use of PEAR in php?

PEAR is known as PHP Extension and Application Repository. It provides structured library to the PHP users and also gives provision for package maintenance.

26.What is the difference between PHP and JavaScript?

The difference lies with the execution of the languages. PHP is server side scripting language, which means that it can’t interact directly with the user. Whereas, JavaScript is client side scripting language, that is used to interact directly with the user

27.What does ODBC do in context with PHP?

PHP supports many databases like dBase, Microsft SQL Server, Oracle, etc. But, it also supports databases like filePro, FrontBase and InterBase with ODBC connectivity. ODBC stands for Open Database connectivity, which is a standard that allows user to communicate with other databases like Access and IBM DB2.

28.Why PHP is sometimes called as embedded scripting language?

PHP is a high level language which is used to allow users to write and understand it in human readable form and also use an interpreter to interpret the code which user write for the computer. PHP is used as an embedded scripting language for the web. PHP is embedded in HTML code. HTML tags are used to enclose the PHP language. HTML is used and PHP is code written in it in the same way as you write JavaScript in HTML.

29.What is the difference between echo, print and printf()?

Echo is the basic type used to print out a string. It just shows the content of the message written using it. It can have multiple parameters as well. print is a construct, it returns TRUE on successful output and FALSE there is no output. It can’t have multiple parameters. Printf() is a function, and not be used as a construct. It allows the string output to be formatted. It is the slowest medium to print the data out.

30.Why IDE is recommended for use while programming with PHP?

IDE stands for Integrated Development environment; it is a framework for developing applications. It includes programming editor where you can edit and write the development programs. The features of IDE are as follows:

  • 1. Debugging: this is the feature which is used to debug or find the bugs in a program.
  • 2. Preview: this is the feature which allow instant preview of the program you are writing.
  • 3. Testing: this is the features that includes built in testing features through which you can check your scripts.
  • 4. FTP: through this you can upload and download the file while connecting to the server.
  • 5. Project management: it organizes scripts into projects; manages the files in the project; includes file checkout and check-in features.
  • 6. Backups: it creates backups automatically of your Web site at periodic intervals.

PHP Interview Question & Answer

1.Explain how to submit form without a submit button in PHP?

A form can be submitted in various ways without using submit button.

  • Submitting a form by selecting an option from drop down box with the invocation of onChange event
  • Using java script : document.form.submit();
  • Using header(“location:page.php”);

2.How can we increase the execution time of a php script?

Default time allowed for the PHP scripts to execute is 30s defined in the php.ini file. The function used is set_time_limit(int seconds). If the value passed is ‘0’, it takes unlimited time. It should be noted that if the default timer is set to 30 sec and 20 sec is specified in set_time_limit(), the script will run for 45 secs.

How can we increase the execution time of a php script?

The script execution time can be increased by

– Using sleep() function in PHP script

– Using set_time_limit() function

– The default limit is 30 seconds. The time limit can be set to zero to impose no time limit to pause.

3.What are the functions for IMAP?

IMAP is used for communicate with mail servers. It has a number of functions. Few of them are listed below:

  • Imap_alerts – Returns all the imap errors occurred
  • Imap_body – Reads the message body
  • Imap_check – Reads the current mail box
  • Imap_clearflag_full – Clears all flags
  • Imap_close – close and IMAP stream
  • Imap_delete – Delete message from current mailbox
  • Imap_delete_mailbox – Deletes a mailbox
  • Imap_fetchbody – Fetches body of message
  • Imap_fetchheader – Fetches header of message
  • Imap_headers – Returns headers for ALL messages
  • Imap_mail : send a mail
  • Imap_sort- Sorts imap messages

4.What is Type juggle in php?

Type Juggling means dealing with a variable type. In PHP a variables type is determined by the context in which it is used. If an integer value is assigned to a variable, it becomes an integer.
E.g. $var3= $var1 + $var2

Here, if $var1 is an integer. $var2 and $var3 will also be treated as integers.

5.What is the difference between mysql_fetch_object and mysql_fetch_array?

Mysql_fetch_object returns the result from the database as objects while mysql_fetch_array returns result as an array. This will allow access to the data by the field names. E.g. using mysql_fetch_object field can be accessed as $result->name and using mysql_fetch_array field can be accessed as $result->[name]

6.Explain the ways to retrieve the data in the result set of MySQL using PHP?

Ways to retrieve the data in the result set of MySQL using PHP

1. mysql_fetch_row($result):- where $result is the result resource returned from a successful query executed using the mysql_query() function.

$result = mysql_query(“SELECT * from students);
while($row = mysql_fetch_row($result))
{
Some statement;
}

2. mysql_fetch_array($result):- Return the current row with both associative and numeric indexes where each column can either be accessed by 0, 1, 2, etc., or the column name.

$row = mysql_fetch_array($result)

3. mysql_fetch_assoc($result): Return the current row as an associative array, where the name of each column is a key in the array.

$row = mysql_fetch_assoc($result)
$row[‘column_name’]

7.What is the difference between the functions unlink and unset in PHP?

The function unlink() is to remove a file, where as unset() is used for destroying a variable that was declared earlier.

8.What is Joomla in PHP?

Joomla is a content management system. Powerful online applications and web sites are build using Joomla. Joomla is an open source CMS tool. Clients can easily manage their web sites with minimal amount of instructions. It is highly extensible. Joomla runs off PHP or MySQL. Joomla is used to create, maintain a structured, flexible portal, add or edit content, changes the look and feel of the site. PHP scripting is used and persisted most of its data / information in MySQL database.unset() empties a variable or contents of file.

9.What is zend engine in PHP?

Zend engine is like a virtual machine and is an open source, and is known for its role in automating the web using PHP. Zend is named after its developers Zeev and Aandi. Its reliability, performance and extensibility has a significant role in increasing the PHP’s popularity. The Zend Engine II is the heart of PHP 5. It is an open source project and freely available under BSD style license.

10.What is the difference between Split and Explode in PHP?

The split() function splits the string into an array using a regular expression and returns an array.
Ex: split(:India:Pakistan:Srilanka); returns an array that contains India, Pakistan, Srilanka.

The explode() function splits the string by string.
Ex: explode(and India and Pakistan and Srilanka); returns an array that contains India, Pakistan, Srilanka.

11.What is the difference between echo and print statement in PHP?

Multiple expressions can be given in echo statement, where as print cannot take multiple expressions.
Echo does not have a return value, where as print returns a value indicating successful execution.
Echo is faster when compared with print.

12.What is CAPTCHA in PHP?

Captcha are images with some characters/ digits on it. One need to type the characters or digits in the text box for the purpose of submitting. This avoids automatic submitting by an operation by other programs or a robot.

13.What is difference between developing website using Java and PHP?

Both technologies are used for dynamic websites development.

PHP is an interpreter based technology where as java is compiler based(usually JSP).

PHP is open source where as JSP is not.

Web sites developed in PHP are much more faster compared to Java technology

Java is a distributed technology, which means N tier application can be developed, where as PHP is used only for web development.

14.How do you create sub domains using PHP?

Wild card domains can be used. Sub domains can be created by first creating a sub directory in the /htdocs folder. E.g. /htdocs/mydomain. Then, the host file needs to be modified to define the sub domain. If the sub domains are not configured explicitly, all requests should be thrown to the main domain.

15.How to upload files using PHP?

– Select a file from the form using <inupt type=”file”>

– Specify the path into which the file is to be stored.

– Insert the following code in php script to upload the file.

move_uploaded_file($_FILES[“file”][“tmp_name”], “myfolder/” . $_FILES[“file”][“name”]);

How to Install WordPress

1 Checking to ensure that you and your web host have the minimum requirements to run WordPress.
2 Download the latest release of WordPress.
3 Unzip the downloaded file to a folder on your hard drive.
4 Be prepared with a secure password for your Secret Key
5 Print this page out so you have it handy during the installation.

Using Cpanel

  1. Log in to your cPanel.
  2. Click MySQL Database Wizard icon under the Databases section.
  3. In Step 1. Create a Database enter the database name and click Next Step.
  4. In Step 2. Create Database Users enter the database user name and the password. Make sure to use a strong password. Click Create User.
  5. In Step 3. Add User to Database click the All Privileges checkbox and click Next Step.
  6. In Step 4. Complete the task note the database name and user. Write down the values of hostname, username, databasename, and the password you chose. (Note that hostname will usually be localhost.)