PHP数据库编程-使用mysql扩展库对数据库操作

WBOY
Release: 2016-06-23 14:37:26
Original
947 people have browsed it

PHP数据库编程-使用mysql扩展库对数据库操作:

<?php    //使用mysql扩展库来操作mysql步骤    //1、获取连接    $conn=mysql_connect("localhost","root","root");    if(!$conn){        die("连接不成功".mysql_error());    }        //2、选择数据库    mysql_select_db("test");    //3、设置操作编码(建议有)    mysql_query("set names utf8"); //保证我们的PHP程序是按照utf8码操作    //4、发送指令sql (ddl 数据定义语句(创建表) dml(数据操作语言 update,insert delete) dql(select查询) dtl(数据事务语句 rollback ...) )    $sql = "select * from user1";        //mysql_query函数返回结果集    $res = mysql_query($sql,$conn);    //var_dump($res);    //5、接收返回结果并处理    while($row=mysql_fetch_row($res)){        //第一种取法        //$row是一个数组        //var_dump($row);        //echo $row[0]."-".$row[1]."-".$row[2]."-".$row[3]."-".$row[4];        //echo "<br/>";        //第二种取法        foreach($row as $key=>$val){            echo "-$val";        }        echo "<br/>";    }    //6、释放资源,关闭连接    mysql_free_result($res);    //关闭数据库可以没有,建议写    mysql_close($conn);?
Copy after login

 

 

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!