Skip to main content

Posts

Showing posts from 2017

PHP include() Vs require()

Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website. For example, if we want to create an instance of a class defined in the separate class file, then it has to be included before creating an instance of it. PHP provides various functions to include external files. The following list of functions is used to include external file into a PHP program.n this tutorial, we are going to compare these functions with suitable examples. Also, we are going to see the purpose of the _once usage and the difference between include and include_once/require and require_once. include() require() include_once() require_once() include(): PHP include() function includes external file into a PHP program. It accepts the external file path and checks if the file exists or not. If the file does not exist in the specified path, then the include() will return PHP warning. Warning : failed to open stream : No such file or directory ... W...

How to view and debug PHP arrays

Sometimes when coding or debugging in PHP, it’s helpful to see the entire contents of an array. An easy way to do this is with the print_r command. To make it more readable, wrap it in <pre> tags. For example, to see the contents of associative array $result: <pre> <? = print_r ( $result ) ?> </pre> You’ll see output like this: Array (     [key1] => value1     [key2] => value2     [key3] => value3 ) 1

How to rename a file or directory in ubuntu from terminal

There is no rename function in Ubuntu Linux. Instead, you simply move the file, giving it a new name. If you don’t actually mv it to another directory, then you have effectively renamed it: mv filename_original filename_new If you are trying to rename a directory, you need to use the -r recursive flag: mv -r original_directoryname new_directoryname

How to Check the Ubuntu Version

How to Check the Ubuntu Version.         Command used : more /etc/issue      This will return something like this:      Output  : Ubuntu 14.04.5 LTS \n \l

Send Email via SMTP server using PHPMailer

Email sending is the most useful feature for web application. mail() function in PHP is used to send mail from PHP script.While sending email using mail() ,the mail will be sent from web server. Sometimes it may cause issues on sending an email and fails to deliver. With SMTP you can overcome this issue,when you send an email through SMTP server email will be sent through mail server  rather than web server.       The easiest way to send email in PHP with SMTP is to use PHPMailer library. PHPMailer provides an ability to send email via SMTP server in PHP. In this tutorial, you’ll show how you can send HTML email with SMTP in PHP using PHPMailer. Send Email via SMTP server: PHPMailer library: The script uses PHPMailer to send the email via SMTP server, so PHPMailer library needs to be included first. Include the PHPMailerAutoload.php file and create a new PHPMailer instance. <?php require ( "phpmailer/PHPMailerAutoload.php" ); $mail = new ...

POPULATE DROPDOWN MENU FROM MYSQL

Populate  drop down menu from MYSQL: 1 2 3 4 5 6 7 8 <select name="user"> <?php $sql = mysqli_query ( $connect , "SELECT name FROM users" ); while ( $row = $sql -> fetch_assoc () ) { echo '<option value = "' . $row [ 'name' ] . '">' . $row [ 'name' ] . "</option>" ; } ?> </select> Show a drop down selected value coming from database: 1 2 3 4 5 6 7 8 9 10 11 <select name="user"> <?php $name = $users [ 'name' ]; $sql = mysqli_query ( $connect , "SELECT name FROM users" ); while ( $row = $sql -> fetch_assoc () ) { $selected = ( $row [ 'name' ] == $name ) ? 'selected="selected"' : '' ; echo '<option value="' . $row [ 'name' ] . '" ' . $selected . '>' . $row [ 'name...

SIMPLE PAGINATION USING PHP AND BOOTSTRAP

Hello guys, today we will learn how to create a simple pagination using PHP and Bootstrap. What is pagination? If you have a form which allows the user to browse through the rows in a database table, what do you do if that table has hundreds or even thousands of rows? It would not be a good idea to show all those rows in a single form, instead you should split the database output into more manageable chunks or 'pages'. There are two things you must do: Decide on the maximum number of database rows that can be included in each page. You may hard code this value, or (my preferred method) you can define it in a variable so that the value may be changed at runtime. You then need to inform the user that other 'pages' are available and provide a mechanism whereby the user is able to select a different 'page' of details. This is nothing but pagination area.                                 This area tells the ...

MYSQL CONNECTION USING PDO WITH PHP

Hello guys, In the previous tutorial we have seen how to connect to MySQL database with PHP using MySQLi. Today we will learn how to connect with PDO. As a PHP programmer we have learned how to access database by using either MySQL and MySQLi. As of PHP 5.1, there's a better way. PHP Data Objects(PDO) provide methods for prepared statements and working with objects that will make you far more productive! PDO Introduction :        PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. Database Support:       The extension can support any database that a PDO driver has been written for. The following drivers currently implement the PDO interface: PDO_CUBRID Cubrid PDO_DBLIB FreeTDS / Microsoft SQL Server / Sybase PDO_FIREBIRD Firebird PDO_I...

MYSQL DATABASE CONNECTION WITH PHP

Hello friends, today we will learn how to connect to  MySQL  database with PHP. This tutorial will require a MySQL database and a web development environment using mysql, apache and php and a simple text editor. The tutorial takes you through establishing a  MySQL  connection using php on a web page, connecting to a  MySQL  table and retrieving the results and displaying them back on the web page. Earlier versions of PHP used the  MySQL  extension.However, this extension was depreciated in 2012. Later can work with  MySQL  database using MySQLi extension (the "i" stands for improved) PDO (PHP Data Objects)        The main difference between these two are if you want to switch your project to another database PDO makes it easy(since it can connect 12 different databases). You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included.In this t...

PHP FROM COMMAND LINE

Hello guys, today we will learn how to write PHP programs through command line. Generally, PHP is use for web applications but  there are occasions where we can use command line such as automatic mail sending, log tracking, cron jobs etc. PHP supports CLI SAPI(Command Line Interface Server API) for executing the script from command line. This SAPI will differ from other interfaces based on the IO practices, configuration defaults, buffering and more. For example, IO practices – it will not support PHP request method (GET, POST). Instead, it uses IO streams. Command line execution time –  In the command line, the execution time limit is unlimited by default. Buffering – implicit_flush is enabled to display output. It also supports output buffering. Some of the PHP CLI SAPI commands: Command Description -i To display command line PHP info. -v To show version details. -r To execute PHP script without delimiters -f To execute PHP file. -l To check the syntax of the...

SET, GET AND UNSET SESSION VARIABLES IN PHP

Session is a way to store information across multiple pages in a website. How to start a session: A session can be started  by using session_start() function.We can set session variable by using PHP global variable $_SESSION. consider an example: create a new page demo1.php and start a new PHP session in this page and set some session variables. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?php // Start the session session_start (); ?> <!DOCTYPE html> <html> <body> <?php // Set session variables $_SESSION [ "username" ] = "sowmya" ; $_SESSION [ "email" ] = "sowmya.vejerla@gmail.com" ; echo 'username : ' . $_SESSION [ 'username' ]; echo '<br>' ; echo 'email : ' . $_SESSION [ 'email' ]; ?> </body> </html> Next we create another page demo2.php ,from this page we will access  the session variables that we set ...