Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

PHP Day 06

http://www.php.net http://www.mysql.com

Geshan Manandhar
Developer,
Young Innovations Private Limited
www.geshanmanandhar.com
GeshanManandhar.com 1
PHP – MYSQL Functions
► $resource_link = mysql_connect
( “server_name”, “user_name”, “password”);
- Open a connection to a MySQL Server.
► mysql_select_db ( “data_base_name”,
$resourece_link ); - Select a MySQL database
► mysql_query ( $query, $resourse_link); -
Send a MySQL query to execute

GeshanManandhar.com 2
PHP – MYSQL Functions
► mysql_fetch_row ( resource $result ); -
Get a result row as an enumerated array.
► mysql_fetch_array ($result,
RESULT_TYPE); - Fetch a result row as an
associative array, a numeric array, or both.
► mysql_num_rows ( resource $result ); -
Get number of rows in result.

GeshanManandhar.com 3
PHP – MYSQL Functions
► mysql_affected_rows (); - Get number of
affected rows in previous MySQL operation.
► mysql_error ( $resource_link ); - Returns
the text of the error message from previous
MySQL operation.
► mysql_close ( $resource_link ); - Close
MySQL connection.

GeshanManandhar.com 4
Insert the User form field values to
tbl_users
► Assumed the form has been submitted with action insert_user_process and
method post.
► db_conn.php
<?php
$db['host'] = "localhost";
$db['user_name'] = "root";
$db['pass_word'] = "";
$db['name'] = "php_class_db";

$connection = mysql_connect ($db['host'], $db['user_name'],$db['pass_word']);

if(!$connection){
die("Error connecting to the database server");
}
$db_name = mysql_select_db($db['name'], $connection);
?>

GeshanManandhar.com 5
User Table structure

GeshanManandhar.com 6
The easy STATIC insert
► “INSERT INTO tbl_user_test ( user_login,
pass_word, address, email, gender, heard_from,
newsletter, created_on, modified_on )
VALUES ('".$fdata['user_login']."', '".
$fdata['pass_word']."', '".$fdata['address']."', '".
$fdata['email']."', '".$fdata['gender']."', '".
$fdata['heard_from']."', '".$fdata['newsletter']."',
'".$fdata['created_on']."', '".
$fdata['modified_on']."');”;

GeshanManandhar.com 7
Insert user function
function insert_user_to_db($fdata){

global $connection;
$tbl_name ="tbl_user_test";
//INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2);
$query = "INSERT into $tbl_name (";

foreach($fdata as $key => $value){


if($key == "modified_on"){
$query .= $key.")";
}
else{
$query .= $key.", ";
}
}//could have done it with array_keys($fdata);

…Continued

GeshanManandhar.com 8
Insert user Function
$query .= " VALUES (";

foreach($fdata as $key => $value){


if($key == "modified_on"){
$query .= "'$value'".");";
}
else{
$query .= "'$value'".", ";
}
}

$result = mysql_query($query, $connection);

if (!$result) {
return 0;
}
else {
return 1;
}
}
► Full code at: day06\insert_user_process.php
GeshanManandhar.com 9
Read some users from database

GeshanManandhar.com 10
Function show users

Code at day06\show_users.php
GeshanManandhar.com 11
Output of show users

GeshanManandhar.com 12
Questions

GeshanManandhar.com 13
Upgrade your Login System
► Create database with db desiger4 and export with
use of PHPMyAdmin.
► Authenticate a user to the login system user select
* from $tbl_name where user_login =
$fdata[‘user_login’];
► Then compare password if it matches login else do
not login.
 if($row[‘pass_word’] == $fdata[‘pass_word’]){
Login
}
► Maintain session very well.

GeshanManandhar.com 14

You might also like