Rumah > php教程 > php手册 > php对mysql数据库简单连接操作

php对mysql数据库简单连接操作

WBOY
Lepaskan: 2016-12-05 13:26:26
asal
1881 orang telah melayarinya

前些阵子忙完了公司前端静态页面的事情了之后,简单学习了下php的基础知识,今天想了想回顾一下php连接数据库的方式,写一下随笔存一下看看

php连接数据库端口和新建数据库

<span style="color: #000000">php
        </span><span style="color: #800080">$servername</span> = "localhost"<span style="color: #000000">;
        </span><span style="color: #800080">$username</span> = "root"<span style="color: #000000">;
        </span><span style="color: #008000">//</span><span style="color: #008000"> 创建连接</span>
        <span style="color: #800080">$conn</span> = <span style="color: #008080">mysqli_connect</span>(<span style="color: #800080">$servername</span>, <span style="color: #800080">$username</span><span style="color: #000000">);
        </span><span style="color: #008000">//</span><span style="color: #008000"> 检测连接</span>
        <span style="color: #0000ff">if</span> (!<span style="color: #800080">$conn</span><span style="color: #000000">) {

            </span><span style="color: #0000ff">die</span>("连接失败: " . <span style="color: #008080">mysqli_connect_error</span><span style="color: #000000">());
        }

        </span><span style="color: #008000">//</span><span style="color: #008000"> 创建数据库</span>
        <span style="color: #800080">$sql</span> = "CREATE DATABASE myDB"<span style="color: #000000">;
        </span><span style="color: #0000ff">if</span> (<span style="color: #008080">mysqli_query</span>(<span style="color: #800080">$conn</span>, <span style="color: #800080">$sql</span><span style="color: #000000">)) {
            </span><span style="color: #0000ff">echo</span> "数据库创建成功"<span style="color: #000000">;
        } </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
            </span><span style="color: #008000">//</span><span style="color: #008000">数据库已经存在</span>
            <span style="color: #0000ff">echo</span> "Error creating database: " . <span style="color: #008080">mysqli_error</span>(<span style="color: #800080">$conn</span><span style="color: #000000">);
        }

        </span><span style="color: #008080">mysqli_close</span>(<span style="color: #800080">$conn</span><span style="color: #000000">);
        </span>?>
Salin selepas log masuk

 

连接成功后,创建简单的数据表

<span style="color: #008000">//</span><span style="color: #008000"> 使用 sql 创建数据表</span>
        <span style="color: #800080">$sql</span> = "<span style="color: #000000">CREATE TABLE MyGuests (
        id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        firstname VARCHAR(30) NOT NULL,
        lastname VARCHAR(30) NOT NULL,
        email VARCHAR(50),
        reg_date TIMESTAMP
        )</span>"<span style="color: #000000">;

        </span><span style="color: #0000ff">if</span> (<span style="color: #800080">$conn</span>->query(<span style="color: #800080">$sql</span>) === <span style="color: #0000ff">TRUE</span><span style="color: #000000">) {
            </span><span style="color: #0000ff">echo</span> "Table MyGuests created successfully"<span style="color: #000000">;
        } </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
            </span><span style="color: #0000ff">echo</span> "创建数据表错误: " . <span style="color: #800080">$conn</span>-><span style="color: #000000">error;
        }</span>
Salin selepas log masuk

 

在连接数据库的时候,要保证数据库的服务器端口是打开的,不然无法连接到,书写代码之前,可以使用navicat对数据库进行测试连接

在学习的时候,打开数据库服务,可以通过wamp的集成环境来进行

 

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan