Home  >  Article  >  Backend Development  >  PHP simple database connection program_PHP tutorial

PHP simple database connection program_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:46:41735browse

A good and simple database connection program. Students who need php mysql database connection class can refer to this article.

The code is as follows
 代码如下 复制代码


require_once("mssql.class.php");
//1.创建类,并连接数据库
$db = new mssql("dns=aaa;uid=sa;pwd=sa;dbname=test");
//2.连接数据库
$conn = $db->config("dns=aaa;uid=sa;pwd=sa;dbname=test");
//3.选择数据库
$dbname = $db->select_db("test");
//4.设置允许调试
$db->debug = true;
//5.执行一条不返回结果的SQL语句
$db->execute("insert into test01(name) values('这是一个测试!')");
//$db->exec("");
//6.执行一条返回结果的SQL语句
$rs = $db->query("select * from test01");
//7.以row方式显示结果
echo "
以row方式显示结果集
";
while($r = $db->fetch_row($rs)){
 echo $r[0].":".$r[1]."
";
}

//8.以array方式显示结果
$rs2 = $db->query("select * from test01");

echo "
以array方式显示结果集
";
while($r = $db->fetch_array($rs2)){
 echo $r["id"] . ":" . $r["name"] . "
";
}

//X.释放
$db->db_close();

?>


 

Copy code

require_once("mssql.class.php");
//1. Create a class and connect to the database
$db = new mssql("dns=aaa;uid=sa;pwd=sa;dbname=test");
//2. Connect to database
$conn = $db->config("dns=aaa;uid=sa;pwd=sa;dbname=test");
//3.Select database
$dbname = $db->select_db("test");
//4.Set to allow debugging
$db->debug = true;
//5. Execute a SQL statement that does not return a result
$db->execute("insert into test01(name) values('This is a test!')");
//$db->exec("");
//6. Execute a SQL statement that returns results
$rs = $db->query("select * from test01");
//7. Display the results in row format
echo "
Display the result set in row format
";
while($r = $db->fetch_row($rs)){
echo $r[0].":".$r[1]."
";
}

//8. Display the results in array format
$rs2 = $db->query("select * from test01");

//X. Release
$db->db_close();
?>
http://www.bkjia.com/PHPjc/632932.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632932.htmlTechArticleA good and simple database connection program. Students who need php mysql database connection class can refer to this article. The code is as follows Copy the code ?PHP require_once(mssql.class.php); //1. Create...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn