trollolodn

#php : #mysql and #xml CRUD application

Mar 18th, 2013
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Latex 2.25 KB | None | 0 0
  1. ***Connection establish :
  2. #Mysql :
  3.    $con = mysql_connect('Server Host Name','User Name' ,'Password');
  4.   mysql_select_db('Database name',$con);
  5. #XML :
  6.    $xml = simplexml_load_file("users.xml"); //This line will load the XML file.
  7.   $sxe = new SimpleXMLElement($xml->asXML());
  8. ***View the details:
  9. #Mysql:
  10.    <?php $query   = "SELECT * FROM users";
  11.           $result  = mysql_query($query);
  12.           while($resUsers = mysql_fetch_assoc($result)){
  13.            echo $resUsers['name']
  14.          } ?>
  15. #XML :
  16.     $rows = count($sxe);
  17.     for($i=0;$i<$no_rows;$i++){
  18.       echo  ($sxe->users[$i]->name);
  19.       }
  20. ***Insert the details:
  21. #Mysql:
  22.    $query = "insert into users(id,name)values($id,'$name')";
  23.    $result = mysql_query($quesry);
  24. #XML:
  25.    $no_rows = count($sxe);
  26.    $query = $sxe->addChild("users");  
  27.    $query->addChild("id", $no_rows+1);
  28.    $query->addChild("name",  ($name));
  29.    $sxe->asXML("users.xml");
  30. ***Edit the details:
  31. #Mysql :
  32.    $query = "select * from users where id=".$edit_id;
  33.    //and update query :
  34.    $query_update = "update users set name = '$name' where id=$edit_id";
  35.    $result = mysql_query($query_update);
  36. #XML:
  37.    <?php $rows = count($sxe);
  38.    for($i = 0, $length = $rows; $i < $length; $i++){
  39.       if($sxe->users[$i]->id == $edit_id){ ?>
  40.       <input value="<?php echo $sxe->users[$i]->name;?>">
  41.       <? }
  42.    //Update:
  43.    $rows = count($sxe);
  44.    for($i = 0, $length = $rows; $i < $length; $i++){
  45.         if($sxe->users[$i]->id == $edit_id){
  46.            $sxe->users[$i]->name =  ($name);
  47.          }
  48.    }
  49.    $sxe->asXML("users.xml");
  50. ***Delete the details:
  51. #Mysql :
  52.    $query = "delete from users where id=".$edit_id;
  53.    $result = mysql_query($query);
  54. #XML :
  55.  
  56.    $rows = count($sxe);
  57.    for($i = 0, $length = $rows; $i < $length; $i++){
  58.        if($sxe->users[$i]->id == $edit_id){
  59.            unset($sxe->users[$i]);
  60.            break;
  61.        }
  62.        }
  63.    $sxe->asXML("users.xml");
  64. ***Database:
  65. CREATE TABLE `users` (
  66. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  67. `name` VARCHAR( 250 ) NOT NULL
  68. )
  69. #XML File:
  70. //(users.xml)
  71. <?xml version="1.0" encoding="iso-8859-1"?>
  72. <userdetails>
  73.     <users>
  74.         <id></id>
  75.         <name></name>
  76.     </users>
  77. </users>
Advertisement
Add Comment
Please, Sign In to add comment