There are lots of different PHP functions. We’ll cover only few of them. Refer to other PHP tutorials when/if you need more PHP functions.
date()
This function returns a local time (date) formatted according to the parameters within the format string. An example:
$date = date(“Y-m-d”);
print “Today is: $date”;
The script would output: Today is: 2001-06-19 assuming today is June 19th, 2001.
mail()
An example:
$email = [email protected];
mail(“$email”, “PHP e-mail example.”, “Message goes here.”);
The above script will send an e-mail to [email protected] with the PHP e-mail example in the subject line of the e-mail message and the Message goes here as the actual message. The mail() function will work only if SENDMAIL is installed on your server. Contact your host before using this function.
And now we are going to learn some MySQL functions. The implementation of a database aids you to keep or change information entered by your users. In order to perform MySQL functions, MySQL must be installed on your server or on your local computer. If you are not familiar with MySQL, read our MySQL tutorial before continuing on with PHP MySQL functions.
Before entering, extracting or updating any user input to our MySQL database, we must connect to the MySQL database. Use the following command:
mysql_connect()
An example:
$hostname = “localhost:3306”;
$username = “yourusername”;
$password = “yourpassword”;
$mysql_connect($hostname, $username, $password);
As you can see, in order to connect to MySQL database we must know the hostname, username and password of the database. The next function closes the last opened database:
mysql_close()
Now let’s create our first table in our database (we’ll call it ‘mytable’ and store the names and e-mails of our website visitors there) using the following complete PHP script:
<?php
$dbname = “databasename”;
$tablename = “mytable”;
mysql_connect(“$hostname”, “$username”, “$password”) or die(“Unable to connect to database”);
@mysql_select_db(“$dbname”) or die(“Unable to select database”);
$query = “CREATE TABLE $tablename (name VARCHAR(25), email VARCHAR(25))”;
$result = mysql_query($query);
print “Table created<BR>”;
mysql_close();
?>
There are several new MySQL functions in the above script.
@mysql_select_db(“our database name”) – this function selects our database on the server.
mysql_query(“query”) – this function sends a query to the MySQL database. We specified our query in the $query variable.
Well, let’s go to the next tutorial PHP MySQL functions.