首页  >  文章  >  后端开发  >  PHP连接MYSQL数据库,phpmysql数据库_PHP教程

PHP连接MYSQL数据库,phpmysql数据库_PHP教程

WBOY
WBOY原创
2016-07-12 09:00:28840浏览

PHP连接MYSQL数据库,phpmysql数据库

因为前两天某项目需要PHP连接MySql,虽然以前学过,但是现在基本也忘得一干二净了,不过后来查找大量资料找到了方法:

<?<span>php
    </span><span>//</span><span>设置字符集</span>
    <span>header</span>('Content-Type: text/html; charset=utf8'<span>);

    </span><span>//</span><span>连接数据库</span>
    <span>$link</span> = <span>mysql_connect</span>("localhost","root","123456"<span>);

    </span><span>if</span>(!<span>$link</span><span>){
        </span><span>echo</span> '数据库连接失败...<br>'<span>;
        </span><span>exit</span>(-1<span>);
    }</span><span>else</span><span>{
        </span><span>echo</span> "数据库连接成功...<br>"<span>;
    }

    </span><span>//</span><span>选择一个数据库作为默认数据库</span>
    <span>mysql_select_db</span>('php_sjk'<span>);

    </span><span>//</span><span>执行sql插入语句</span>
    <span>$sql</span> = "INSERT INTO zh VALUES (NULL,'王杰','123456')"<span>;
    </span><span>$cr</span> = <span>mysql_query</span>(<span>$sql</span><span>);

    </span><span>//</span><span>判断是否插入成功</span>
    <span>if</span>(!<span>$cr</span><span>){
        </span><span>echo</span> "数据插入失败。<br>"<span>;
    }</span><span>else</span><span>{
        </span><span>echo</span> "数据插入成功。<br>"<span>;
    }
    
    </span><span>//</span><span>执行sql查询语句</span>
    <span>$sql</span> = "select * from zh"<span>;
    </span><span>$cr</span> = <span>mysql_query</span>(<span>$sql</span><span>);
    
    </span><span>//</span><span>得到返回的结果集,并循环输出</span>
    <span>echo</span> "数据展示:<br><br><br>"<span>;
    </span><span>while</span>(<span>$a</span> = <span>mysql_fetch_row</span>(<span>$cr</span><span>)){
        </span><span>echo</span> "{<span>$a</span>[0]}&mdash;&mdash;{<span>$a</span>[1]}&mdash;&mdash;{<span>$a</span>[2]}<br>"<span>;
    }

    </span><span>//</span><span>关闭数据库连接</span>
    <span>mysql_close</span>(<span>$link</span><span>);
</span>?>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1093868.htmlTechArticlePHP连接MYSQL数据库,phpmysql数据库 因为前两天某项目需要PHP连接MySql,虽然以前学过,但是现在基本也忘得一干二净了,不过后来查找大量资...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn