Your SlideShare is downloading. ×
06 Php Mysql Connect Query
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

06 Php Mysql Connect Query

1,314
views

Published on

Simple connect to MYSQL from PHP and some simple queries.

Simple connect to MYSQL from PHP and some simple queries.

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,314
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. PHP Day 06 Geshan Manandhar Developer, Young Innovations Private Limited www.geshanmanandhar.com http://www.php.net http://www.mysql.com
  • 2. 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
  • 3. 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.
  • 4. 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.
  • 5. 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'] = &quot;localhost&quot;;
    • $db['user_name'] = &quot;root&quot;;
    • $db['pass_word'] = &quot;&quot;;
    • $db['name'] = &quot;php_class_db&quot;;
    • $connection = mysql_connect ($db['host'], $db['user_name'],$db['pass_word']);
    • if(!$connection){
    • die(&quot;Error connecting to the database server&quot;);
    • }
    • $db_name = mysql_select_db($db['name'], $connection);
    • ?>
  • 6. User Table structure
  • 7. The easy STATIC insert
    • “ INSERT INTO tbl_user_test ( user_login, pass_word, address, email, gender, heard_from, newsletter, created_on, modified_on )
    • VALUES ('&quot;.$fdata['user_login'].&quot;', '&quot;.$fdata['pass_word'].&quot;', '&quot;.$fdata['address'].&quot;', '&quot;.$fdata['email'].&quot;', '&quot;.$fdata['gender'].&quot;', '&quot;.$fdata['heard_from'].&quot;', '&quot;.$fdata['newsletter'].&quot;', '&quot;.$fdata['created_on'].&quot;', '&quot;.$fdata['modified_on'].&quot;');”;
  • 8. Insert user function
    • function insert_user_to_db($fdata){
    • global $connection;
    • $tbl_name =&quot;tbl_user_test&quot;;
    • //INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2);
    • $query = &quot;INSERT into $tbl_name (&quot;;
    • foreach($fdata as $key => $value){
    • if($key == &quot;modified_on&quot;){
    • $query .= $key.&quot;)&quot;;
    • }
    • else{
    • $query .= $key.&quot;, &quot;;
    • }
    • }//could have done it with array_keys($fdata);
    • … Continued
  • 9. Insert user Function
    • $query .= &quot; VALUES (&quot;;
    • foreach($fdata as $key => $value){
    • if($key == &quot;modified_on&quot;){
    • $query .= &quot;'$value'&quot;.&quot;);&quot;;
    • }
    • else{
    • $query .= &quot;'$value'&quot;.&quot;, &quot;;
    • }
    • }
    • $result = mysql_query($query, $connection);
    • if (!$result) {
    • return 0;
    • }
    • else {
    • return 1;
    • }
    • }
    • Full code at: day06insert_user_process.php
  • 10. Read some users from database
  • 11. Function show users Code at day06show_users.php
  • 12. Output of show users
  • 13. Questions
  • 14. 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.