06 Php Mysql Connect Query

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    06 Php Mysql Connect Query - Presentation 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.

    + Geshan ManandharGeshan Manandhar, 5 months ago

    custom

    560 views, 0 favs, 0 embeds more stats

    Simple connect to MYSQL from PHP and some simple qu more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 560
      • 560 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 0
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories