Home >Backend Development >PHP Tutorial >PHP uses mysqli to connect to the database

PHP uses mysqli to connect to the database

WBOY
WBOYOriginal
2016-08-08 09:23:551071browse

Mysql has been used for database connection in the past, but with the development of PHP, it has gradually abandoned the trend of mysql series functions and switched to mysqli series functions. Here is a demo using mysqli for database:

<code><span>$conn</span> = mysqli_connect(<span>'localhost'</span>, <span>'root'</span>, <span>''</span>, <span>'addressBook'</span>) or die(<span>'Connect error!'</span>);

        <span>$sql</span> = <span>"insert into "</span>.<span>$this</span>->tb_group.<span>" value('', '<span>$name</span>', '<span>$pwd</span>', '<span>$numIdList</span>')"</span>;

        <span>$result</span> = <span>$conn</span>->query(<span>$sql</span>);        //添加成功result的值为boolean值<span>true</span>
        //<span>$result</span> = mysqli_query(<span>$conn</span>, <span>$sql</span>);

        <span>if</span> (<span>$result</span>) {
            <span>$lastid</span> = mysqli_insert_id(<span>$conn</span>);    //刚刚插入数据库的记录id
            <span>echo</span><span>"<BR>新建通讯录成功!"</span>;
        }<span>else</span>{
            <span>echo</span><span>"<BR>新建通讯录失败!"</span>;
        }</code>

The above introduces how PHP uses mysqli to connect to the database, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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
Previous article:Nginx common commandsNext article:Nginx common commands