Home >Backend Development >PHP Tutorial >PHP database operation mysqli

PHP database operation mysqli

WBOY
WBOYOriginal
2016-07-29 09:01:471248browse

Database link

<code><span>$server</span> = <span>"127.0.0.1"</span>;
<span>$username</span> = <span>"username"</span>;
<span>$pass</span> = <span>"password"</span>;
<span>$con</span> = new mysqli(<span>$server</span>,<span>$username</span>,<span>$pass</span>[,<span>$db_name</span>]);
///创建一个数据库链接,如果带上后面参数 <span>$db_name</span> 创建一个到数据库<span>$db_name</span>的链接,如果后面不带参数,创建一个到server的链接,在后面可以使用 <span>$con</span> -> select_db(<span>$db_name</span>);来选择数据表</code>

Create database

<code><span>try</span>{
    <span>$con</span> -> query(<span>$create_db</span>);
}<span>catch</span> (<span>exception</span><span>$e</span>){
}
<span>$con</span> -> select_db(<span>"nwpu"</span>);
<span>///使用try 可以避免重复创建数据库报错,</span></code>

Create data table

<code>$table = "<span><span>create</span><span>table</span> name(<span>".
        "</span><span>first</span><span>varchar</span>(<span>20</span>) <span>PRIMARY</span><span>KEY</span>,<span>".
        "</span>sec <span>VARCHAR</span>(<span>20</span>)<span>".
    "</span>)<span>";
$con -> query($table);
///使用query执行sql语句</span></span></code>

Close database

<code><span>$con</span> -> <span>close</span>();</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the PHP database operation mysqli, including the relevant content. 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