06 Php Mysql Connect Query

1,582 views

Published on

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,582
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

06 Php Mysql Connect Query

  1. 1. PHP Day 06 Geshan Manandhar Developer, Young Innovations Private Limited www.geshanmanandhar.com http://www.php.net http://www.mysql.com
  2. 2. PHP – MYSQL Functions <ul><li>$resource_link = mysql_connect ( “server_name”, “user_name”, “password”); - Open a connection to a MySQL Server. </li></ul><ul><li>mysql_select_db ( “data_base_name”, $resourece_link ); - Select a MySQL database </li></ul><ul><li>mysql_query ( $query, $resourse_link); - Send a MySQL query to execute </li></ul>
  3. 3. PHP – MYSQL Functions <ul><li>mysql_fetch_row ( resource $result ); - Get a result row as an enumerated array. </li></ul><ul><li>mysql_fetch_array ($result, RESULT_TYPE); - Fetch a result row as an associative array, a numeric array, or both. </li></ul><ul><li>mysql_num_rows ( resource $result ); - Get number of rows in result. </li></ul>
  4. 4. PHP – MYSQL Functions <ul><li>mysql_affected_rows (); - Get number of affected rows in previous MySQL operation. </li></ul><ul><li>mysql_error ( $resource_link ); - Returns the text of the error message from previous MySQL operation. </li></ul><ul><li>mysql_close ( $resource_link ); - Close MySQL connection. </li></ul>
  5. 5. Insert the User form field values to tbl_users <ul><li>Assumed the form has been submitted with action insert_user_process and method post. </li></ul><ul><li>db_conn.php </li></ul><ul><li><?php </li></ul><ul><li>$db['host'] = &quot;localhost&quot;; </li></ul><ul><li>$db['user_name'] = &quot;root&quot;; </li></ul><ul><li>$db['pass_word'] = &quot;&quot;; </li></ul><ul><li>$db['name'] = &quot;php_class_db&quot;; </li></ul><ul><li>$connection = mysql_connect ($db['host'], $db['user_name'],$db['pass_word']); </li></ul><ul><li>if(!$connection){ </li></ul><ul><li>die(&quot;Error connecting to the database server&quot;); </li></ul><ul><li>} </li></ul><ul><li>$db_name = mysql_select_db($db['name'], $connection); </li></ul><ul><li>?> </li></ul>
  6. 6. User Table structure
  7. 7. The easy STATIC insert <ul><li>“ INSERT INTO tbl_user_test ( user_login, pass_word, address, email, gender, heard_from, newsletter, created_on, modified_on ) </li></ul><ul><li>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;');”; </li></ul>
  8. 8. Insert user function <ul><li>function insert_user_to_db($fdata){ </li></ul><ul><li>global $connection; </li></ul><ul><li>$tbl_name =&quot;tbl_user_test&quot;; </li></ul><ul><li>//INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2); </li></ul><ul><li>$query = &quot;INSERT into $tbl_name (&quot;; </li></ul><ul><li>foreach($fdata as $key => $value){ </li></ul><ul><li>if($key == &quot;modified_on&quot;){ </li></ul><ul><li>$query .= $key.&quot;)&quot;; </li></ul><ul><li>} </li></ul><ul><li>else{ </li></ul><ul><li>$query .= $key.&quot;, &quot;; </li></ul><ul><li>} </li></ul><ul><li>}//could have done it with array_keys($fdata); </li></ul><ul><li>… Continued </li></ul>
  9. 9. Insert user Function <ul><li>$query .= &quot; VALUES (&quot;; </li></ul><ul><li>foreach($fdata as $key => $value){ </li></ul><ul><li>if($key == &quot;modified_on&quot;){ </li></ul><ul><li>$query .= &quot;'$value'&quot;.&quot;);&quot;; </li></ul><ul><li>} </li></ul><ul><li>else{ </li></ul><ul><li>$query .= &quot;'$value'&quot;.&quot;, &quot;; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li> $result = mysql_query($query, $connection); </li></ul><ul><li>if (!$result) { </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>return 1; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Full code at: day06insert_user_process.php </li></ul>
  10. 10. Read some users from database
  11. 11. Function show users Code at day06show_users.php
  12. 12. Output of show users
  13. 13. Questions
  14. 14. Upgrade your Login System <ul><li>Create database with db desiger4 and export with use of PHPMyAdmin. </li></ul><ul><li>Authenticate a user to the login system user select * from $tbl_name where user_login = $fdata[‘user_login’]; </li></ul><ul><li>Then compare password if it matches login else do not login. </li></ul><ul><ul><li>if($row[‘pass_word’] == $fdata[‘pass_word’]){ </li></ul></ul><ul><ul><li>Login </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Maintain session very well. </li></ul>

×