PHP Forms Hand
PHP Forms Hand
Compiled By:
Hayelom M. Gebrye
Contents
MYSQL Functions
User Defined Function
PHP MySQL Connection
Integrating Web Forms And Databases
Displaying, inserting and deleting Queries
In Tables
MYSQL Functions
A function is a block of statements that can be
used repeatedly in a program.
A function will not execute immediately when
a page loads. A function will be executed by a
call to the function.
Inbuilt Function
User Defined Function
Inbuilt Function:
There are several Inbuilt functions are available in
php.
Generally all its are single line functions.
User Defined Function
A user defined function declaration starts with the word "function":
Syntax
function function_Name()
{
to be executed;
}
Function names are NOT case-sensitive.
<?php
function fun()
{
echo "Hello world!";
}
fun(); // call the function
?>
PHP MySQL Connection
PHP provides mysql_connect() function to
open a database connection.
This function takes three parameters and
returns a MySQL link identifier on success or
FALSE on failure.
There are some MySQL functions for database
connectivity such as
Mysql_connection()
mysql_select_db()
mysql_query()
mysql_result()
mysql_close()
PHP mysql_connection() Function
PHP mysql_connect() function is used to connect with
MySQL database.
It returns resource if connection is established or null.
Syntax of mysql_connection
User:
Optional - The username accessing the database.
If not specified then default is the name of the user that
owns the server process.
password :
Optional - The password of the user accessing the
database.
If not specified then default is an empty password.
PHP mysql_select_db() Function
The mysql_select_db() function sets the active
MySQL database.
This function returns TRUE on success, or FALSE on
failure.
mysql_create_db() function attempts to create a
new database on the server associated with the
specified link identifier.
The mysql_select_db() function is used to change
the default database for the connection.
Syntax of mysql_select_db() function
mysql_select_db(connection, name)
connection:
Required. Specifies the MySQL connection to use
name:
Required. Specifies the database name
PHP mysql_query() Function
mysql_query() Function performs a query
against a database.
This function selects a database and executes
a query on it.
If the optional link identifier isn't specified, the
function will try to find an open link to the
MySQL server and
if no such link is found it'll try to create one as
if mysql_connect() was called with no
arguments.
Cont..
The mysql_query() sends a query to the
currently active database on the server that's
associated with the specified link identifier.
This function returns a resource identifier or
FALSE if the query was not executed correctly.
Syntax of mysql_query() function
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'mydb');
define('DB_USER','root');
define('DB_PASSWORD','');
$test = $_POST['id'];
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD)
or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to
connect to MySQL: " . mysql_error());
$sql = "DELETE from student_info WHERE id=$test";
if (mysql_query($sql)) {
echo "Record Deleted successfully"; }
else {
echo "Error deleting record: " .mysql_error($con); }
?>
End
Questions?