使用php访问sqlserver2000数据库
在php中调用 phpinfo() 函数,查看运行结果中的“Configuration File (php.ini) Path”一项(配置文件路径)。
在我的电脑中运行如图,然后打开此配置文件。
在config中查找“extension=php_mssql.dll”,如果找到,则去掉前面的";",如果没找到,则添加一行。
在config中查找“extension_dir”一项的值,并打开此路径。
在打开的文件夹中查找是否有文件“php_mssql.dll” ,如果没有,从以前c盘中的php文件中查找并copy一份到当前目录。
把所有打开的文件以及文件夹关闭。配置完成。
下面开始写代码:
--------------------------------------------------------------------------------
首先创建数据库phptest,再创建表tasks,如:(本机数据库用户为“sa”,密码为空)
下列代码,保存到本地,并运行tasklist.php查看效果。
-------------------------- taskadd.php -------------------------
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <title>添加任务</title>
5 </head>
6 <body>
7 <?php
8
9 function display_form() {
10 global $PHP_SELF;
11
12 ?>
13 <form id="form1" action="<?php echo $PHP_SELF; ?>" method="post">
14 <table>
15 <tr>
16 <td>
17 描述</td>
18 <td>
19 <input id="Text1" type="text" name="description" /></td>
20 </tr>
21 <tr>
22 <td>
23 开始日期</td>
24 <td>
25 <input id="Text2" type="text" name="startdate" /></td>
26 </tr>
27 <tr>
28 <td>
29 结束日期</td>
30 <td>
31 <input id="Text3" type="text" name="enddate" /></td>
32 </tr>
33 <tr>
34 <td colspan="2">
35 <input type="hidden" name="stage" value= "results" />
36 <input type="submit" value="保存" />
37 <input type="reset" value="取消" /></td>
38 </tr>
39 </table>
40 </form>
41
42 <?php
43
44 }
45
46 ?>
47 <?php
48
49 function process_form()
50 {
51 $description = $_POST['description'];
52 $startdate = $_POST['startdate'];
53 $enddate = $_POST['enddate'];
54
55 echo $description . "<br>";
56 echo $startdate . "<br>";
57 echo $enddate . "<br>";
58
59 $connect = mssql_connect(".", "sa", "") or die("<br>数据库连接失败!<br>");
60 //echo "数据库连接成功<br>";
61
62 mssql_select_db("phptest", $connect);
63
64 $query = "INSERT INTO [tasks]([task_desc], [task_date], [task_start], [task_end], [task_stats]) VALUES('" . $description . "', getdate(), '" . $startdate . "', '" . $enddate . "', '等待')";
65 echo $query;
66 mssql_query($query, $connect) or die("<br>insert失败!<br>");
67 echo "<br>添加成功";
68
69 mssql_close($connect);
70
71 header("location:tasklist.php");
72 exit;
73 }
74
75 ?>
76 <?php
77
78 $stage = $_POST['stage'];
79 if (empty($stage))
80 {
81 display_form();
82 }
83 else
84 {
85 echo "no one";
86 echo "<br>";
87 process_form();
88 }
89
90 ?>
91 </body>
92 </html>
-------------------------- tasklist.php -------------------------
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" >
3 <head>
4 <title>任务列表</title>
5 </head>
6 <body>
7 <script type="text/javascript">
8
9 function deleteTask(taskid, taskdesc)
10 {
11 if(confirm("确定删除任务? - " + taskid + "\n\n" + taskdesc))
12 {
13 document.getElementById("currid").value = taskid;
14 }
15 else
16 {
17 return false;
18 }
19 }
21 </script>
22 <?php
23 $deleteid = $_POST['deleteitemid'];
24 if(!empty($deleteid))
25 {
26 $connect = mssql_connect(".", "sa", "") or die("数据库连接失败!<br>");
27
28 mssql_select_db("phptest", $connect);
29 $query = "DELETE FROM [phptest].[dbo].[tasks] WHERE task_id='" . $deleteid . "'";
30 mssql_query($query, $connect) or die("<br>delete失败!<br>");
31 echo "删除成功<br>";
32
33 mssql_close($connect);
34 }
35
36 ?>
37 <form id="form1" action="tasklist.php" method="post">
38 任务列表<a href="taskadd.php">新增任务</a><br />
39 <table cellpadding="0" cellspacing="0" border="1px">
40 <tr>
41 <td>
42 描述</td>
43 <td>
44 日期</td>
45 <td>
46 开始</td>
47 <td>
48 结束</td>
49 <td>
50 状态</td>
51 <td>
52 </td>
53 </tr>
54 <?php
55 $connect = mssql_connect(".", "sa", "") or die("数据库连接失败!<br>");
56 //echo "数据库连接成功<br>";
57
58 mssql_select_db("phptest", $connect);
59
60 $query = "select * from tasks order by task_date desc";
61
62 $rs = mssql_query($query, $connect) or die("查询失败!<br>");
63
64 do {
65 while ($row = mssql_fetch_row($rs)) {
66 print("<tr>");
67 print("<td>");
68 echo $row[1];
69 print("</td>");
70 print("<td>");
71 echo $row[2];
72 print("</td>");
73 print("<td>");
74 echo $row[3];
75 print("</td>");
76 print("<td>");
77 echo $row[4];
78 print("</td>");
79 print("<td>");
80 echo $row[5];
81 print("</td>");
82 print("<td><input id='delete" . $row[0] . "' type='submit' value='删除' onclick=\"return deleteTask('" . $row[0] . "', '" . $row[1] . "');\" />");
83 print("</td>");
84 print("</tr>");
85 }
86 } while (mssql_next_result($rs));
87
88 mssql_free_result($rs);
89 mssql_close($connect);
90
91 ?>
92 </table>
93 <input id='currid' type='hidden' name="deleteitemid" />
94 </form>
95 </body>
96 </html>