前些陣子忙完了公司前端靜態頁面的事情了之後,簡單學習了下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>?>
連線成功後,建立簡單的資料表
<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>
在連接資料庫的時候,要確保資料庫的伺服器連接埠是開啟的,不然無法連接到,在書寫程式碼之前,可以使用navicat對資料庫進行測試連線
在學習的時候,開啟資料庫服務,可以透過wamp的整合環境來進行