2014-03-28 83 views
1

我從easyuihttp://www.jeasyui.com/tutorial/app/crud.php該用戶表文件PHP的,但我不知道如何連接到MySQL如何表連接到MySQL

users.php

<style type="text/css"> 
     #fm{ 
      margin:0; 
      padding:10px 30px; 
     } 
     .ftitle{ 
      font-size:14px; 
      font-weight:bold; 
      color:#666; 
      padding:5px 0; 
      margin-bottom:10px; 
      border-bottom:1px solid #ccc; 
     } 
     .fitem{ 
      margin-bottom:5px; 
     } 
     .fitem label{ 
      display:inline-block; 
      width:80px; 
     } 
    </style> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"></script> 
    <script type="text/javascript" src="http://www.jeasyui.com/easyui/jquery.easyui.min.js"></script> 
    <script type="text/javascript"> 
     var url; 
     function newUser(){ 
      $('#dlg').dialog('open').dialog('setTitle','New User'); 
      $('#fm').form('clear'); 
      url = 'save_user.php'; 
     } 
     function editUser(){ 
      var row = $('#dg').datagrid('getSelected'); 
      if (row){ 
       $('#dlg').dialog('open').dialog('setTitle','Edit User'); 
       $('#fm').form('load',row); 
       url = 'update_user.php?id='+row.id; 
      } 
     } 
     function saveUser(){ 
      $('#fm').form('submit',{ 
       url: url, 
       onSubmit: function(){ 
        return $(this).form('validate'); 
       }, 
       success: function(result){ 
        var result = eval('('+result+')'); 
        if (result.success){ 
         $('#dlg').dialog('close');  // close the dialog 
         $('#dg').datagrid('reload'); // reload the user data 
        } else { 
         $.messager.show({ 
          title: 'Error', 
          msg: result.msg 
         }); 
        } 
       } 
      }); 
     } 
     function removeUser(){ 
      var row = $('#dg').datagrid('getSelected'); 
      if (row){ 
       $.messager.confirm('Confirm','Are you sure you want to remove this user?',function(r){ 
        if (r){ 
         $.post('remove_user.php',{id:row.id},function(result){ 
          if (result.success){ 
           $('#dg').datagrid('reload'); // reload the user data 
          } else { 
           $.messager.show({ // show error message 
            title: 'Error', 
            msg: result.msg 
           }); 
          } 
         },'json'); 
        } 
       }); 
      } 
     } 
    </script> 

    <h2>Basic CRUD Application</h2> 
    <div class="demo-info" style="margin-bottom:10px"> 
     <div class="demo-tip icon-tip">&nbsp;</div> 
     <div>Click the buttons on datagrid toolbar to do crud actions.</div> 
    </div> 

    <table id="dg" title="My Users" class="easyui-datagrid" style="width:700px;height:250px" 
      url="get_users.php" 
      toolbar="#toolbar" pagination="true" 
      rownumbers="true" fitColumns="true" singleSelect="true"> 
     <thead> 
      <tr> 
       <th field="firstname" width="50">First Name</th> 
       <th field="lastname" width="50">Last Name</th> 
       <th field="phone" width="50">Phone</th> 
       <th field="email" width="50">Email</th> 
      </tr> 
     </thead> 
    </table> 
    <div id="toolbar"> 
     <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a> 
     <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a> 
     <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeUser()">Remove User</a> 
    </div> 

    <div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px" 
      closed="true" buttons="#dlg-buttons"> 
     <div class="ftitle">User Information</div> 
     <form id="fm" method="post" novalidate> 
      <div class="fitem"> 
       <label>First Name:</label> 
       <input name="firstname" class="easyui-validatebox" required="true"> 
      </div> 
      <div class="fitem"> 
       <label>Last Name:</label> 
       <input name="lastname" class="easyui-validatebox" required="true"> 
      </div> 
      <div class="fitem"> 
       <label>Phone:</label> 
       <input name="phone"> 
      </div> 
      <div class="fitem"> 
       <label>Email:</label> 
       <input name="email" class="easyui-validatebox" validType="email"> 
      </div> 
     </form> 
    </div> 
    <div id="dlg-buttons"> 
     <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">Save</a> 
     <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">Cancel</a> 
    </div> 

conn.php

<?php 

$conn = @mysql_connect('127.0.0.1','root','root'); 
if (!$conn) { 
    die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db('mydb', $conn); 

?> 

get_users.php

<?php 
    $page = isset($_POST['page']) ? intval($_POST['page']) : 1; 
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10; 
    $offset = ($page-1)*$rows; 
    $result = array(); 

    include 'conn.php'; 

    $rs = mysql_query("select count(*) from users"); 
    $row = mysql_fetch_row($rs); 
    $result["total"] = $row[0]; 
    $rs = mysql_query("select * from users limit $offset,$rows"); 

    $items = array(); 
    while($row = mysql_fetch_object($rs)){ 
     array_push($items, $row); 
    } 
    $result["rows"] = $items; 

    echo json_encode($result); 

?> 

這MySQL數據庫

CREATE TABLE `users` (
    `id` int(11) NOT NULL auto_increment, 
    `firstname` varchar(50) default NULL, 
    `lastname` varchar(50) default NULL, 
    `phone` varchar(200) default NULL, 
    `email` varchar(200) default NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
+0

MySQL是過時,使用PDO來代替。使用@並不是最佳實踐。 – Hooli

回答

0

高興的是,它沒有連接。停止使用mysql_*函數,因爲它們已被棄用。切換到PDO

像這樣重寫你的連接。

try 
{ 
    $dbh = new PDO("mysql:host=localhost;dbname=$dbname",$dbuser,$dbpass); 
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
} 
catch (PDOException $e) 
{ 
    echo 'Connection failed: ' . $e->getMessage(); 
} 

How to write queries using PDO ?

+0

哪裏需要添加我的數據庫詳細信息? (鬃毛數據庫,密碼,主機等) – user3462476

+0

請參閱編輯答案和提供的鏈接。 –

+0

//配置 $ dbhost =「localhost」; $ dbname =「my_db_name」; $ dbuser =「my_db_user」; $ dbpass =「my_db_pass」; $ dbh = new PDO(「mysql:host = localhost; dbname = $ dbname」,$ dbuser,$ dbpass); $ dbh-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION); (PDOException $ e) { } echo'連接失敗:'。 $ E->的getMessage(); } – user3462476