设为首页 加入收藏

TOP

如何用PHP连接MySQL
2014-11-24 01:43:26 来源: 作者: 【 】 浏览:19
Tags:何用 PHP 连接 MySQL

1)连接:&conn=mysql_connect(“localhost”, “joeuser”, “somepass”);


2)关闭连接:mysql_close($conn);


3) 数据库与连接建立联系:mysql_select_db(database name, connection index);


4) 将SQL语句给MySQL执行:$result = mysql_query($sql, $conn); //增删改查都是这句


5) 检索数据:返回记录数:$number_of_rows = mysql_num_rows($result);


将记录放入数组:$newArray = mysql_fetch_array($result);


例子:


1: < php
2: // open the connection
3: $conn = mysql_connect(“localhost”, “joeuser”, “somepass”);
4: // pick the database to use
5: mysql_select_db(“testDB”,$conn);
6: // create the SQL statement
7: $sql = “SELECT * FROM testTable”;
8: // execute the SQL statement
9: $result = mysql_query($sql, $conn) or die(mysql_error());
10: //go through each row in the result set and display data
11: while ($newArray = mysql_fetch_array($result)) {
12: // give a name to the fields
13: $id = $newArray['id'];
14: $testField = $newArray['testField'];
15: //echo the results onscreen
16: echo “The ID is $id and the text is $testField
”;
17: }
18: >


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇线形表a、b为两个有序升序的线形.. 下一篇数据库乱码的原因和解决

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: