Home >Backend Development >PHP Tutorial >PHP uses mysqli to connect to the database
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.