首頁 > 後端開發 > php教程 > PHP操作資料庫

PHP操作資料庫

angryTom
發布: 2023-04-07 15:08:01
轉載
7138 人瀏覽過

連接資料庫

error_reporting(0);
$dbhost = 'localhost';  // mysql服务器主机地址
$dbuser = 'root';            // mysql用户名
$dbpass = '123456';          // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('数据库连接失败');
}
echo '数据库连接成功';
登入後複製

#建立資料庫(推薦學習:《mysql影片教學》

$sql = 'CREATE DATABASE RUNOOB';
$retval = mysqli_query($conn,$sql );
if(! $retval )
{
    die('数据库创建失败');
}
echo "数据库创建成功";
登入後複製

刪除資料庫

$sql = 'DROP DATABASE RUNOOB';
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('数据库删除失败');
}
echo "数据库删除成功";
登入後複製

選擇資料庫

mysqli_select_db($conn, 'RUNOOB' );
登入後複製

資料型別

數值、日期/時間、字串

数值类型
tinyint
smallint
medium
int/integer
bigint
float
double
decimal
日期/时间
date
time
datetime
timestamp
字符串
char
varchar
tinyblob
tinytext
blob
text
mediumblob
mediumtext
longblob
longtext
登入後複製

建立資料表

$sql = "CREATE TABLE runoob_tbl( ".
        "runoob_id INT NOT NULL AUTO_INCREMENT, ".
        "runoob_title VARCHAR(100) NOT NULL, ".
        "runoob_author VARCHAR(40) NOT NULL, ".
        "submission_date DATE, ".
        "PRIMARY KEY ( runoob_id ))ENGINE=InnoDB DEFAULT CHARSET=utf8; ";
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('创建数据表失败');
}
echo "创建数据表成功";
登入後複製

刪除資料表

$sql = "DROP TABLE runoob_tbl";
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
  die('删除数据表失败');
}
echo "删除数据表成功";
登入後複製

#插入資料

// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");
 
$runoob_title = '学习 Python';
$runoob_author = 'RUNOOB.COM';
$submission_date = '2016-03-06';
 
$sql = "INSERT INTO runoob_tbl ".
        "(runoob_title,runoob_author, submission_date) ".
        "VALUES ".
        "('$runoob_title','$runoob_author','$submission_date')";
 
 
 
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
  die('插入数据失败');
}
echo "数据插入成功";
登入後複製

查詢資料

// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");
 
$sql = 'SELECT runoob_id, runoob_title, 
        runoob_author, submission_date
        FROM runoob_tbl';
 
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('无法读取数据');
}
echo &#39;<h2>菜鸟教程 mysqli_fetch_array 测试<h2>&#39;;
echo &#39;<table border="1"><tr><td>教程 ID</td><td>标题</td><td>作者</td><td>提交日期</td></tr>&#39;;
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
    echo "<tr><td> {$row[&#39;runoob_id&#39;]}</td> ".
         "<td>{$row[&#39;runoob_title&#39;]} </td> ".
         "<td>{$row[&#39;runoob_author&#39;]} </td> ".
         "<td>{$row[&#39;submission_date&#39;]} </td> ".
         "</tr>";
}
echo &#39;</table>&#39;;
登入後複製

#WHERE

// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");
 
// 读取 runoob_author 为 RUNOOB.COM 的数据
$sql = &#39;SELECT runoob_id, runoob_title, 
        runoob_author, submission_date
        FROM runoob_tbl
        WHERE runoob_author="RUNOOB.COM"&#39;;
 
mysqli_select_db( $conn, &#39;RUNOOB&#39; );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die(&#39;无法读取数据: &#39; . mysqli_error($conn));
}
echo &#39;<h2>菜鸟教程 MySQL WHERE 子句测试<h2>&#39;;
echo &#39;<table border="1"><tr><td>教程 ID</td><td>标题</td><td>作者</td><td>提交日期</td></tr>&#39;;
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
    echo "<tr><td> {$row[&#39;runoob_id&#39;]}</td> ".
         "<td>{$row[&#39;runoob_title&#39;]} </td> ".
         "<td>{$row[&#39;runoob_author&#39;]} </td> ".
         "<td>{$row[&#39;submission_date&#39;]} </td> ".
         "</tr>";
}
echo &#39;</table>&#39;;
// 释放内存
mysqli_free_result($retval);
登入後複製

UPDATE

// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");
 
$sql = &#39;UPDATE runoob_tbl
        SET runoob_title="学习 Python"
        WHERE runoob_id=3&#39;;
 
mysqli_select_db( $conn, &#39;RUNOOB&#39; );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die(&#39;无法更新数据: &#39; . mysqli_error($conn));
}
echo &#39;数据更新成功!&#39;;
登入後複製

DELETE

#
// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");
 
$sql = &#39;DELETE FROM runoob_tbl
        WHERE runoob_id=3&#39;;
 
mysqli_select_db( $conn, &#39;RUNOOB&#39; );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die(&#39;无法删除数据: &#39; . mysqli_error($conn));
}
echo &#39;数据删除成功!&#39;;
登入後複製

##LINK

###########
// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");
 
$sql = &#39;SELECT runoob_id, runoob_title, 
        runoob_author, submission_date
        FROM runoob_tbl
        WHERE runoob_author LIKE "%COM"&#39;;
 
mysqli_select_db( $conn, &#39;RUNOOB&#39; );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die(&#39;无法读取数据: &#39; . mysqli_error($conn));
}
echo &#39;<h2>菜鸟教程 mysqli_fetch_array 测试<h2>&#39;;
echo &#39;<table border="1"><tr><td>教程 ID</td><td>标题</td><td>作者</td><td>提交日期</td></tr>&#39;;
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
    echo "<tr><td> {$row[&#39;runoob_id&#39;]}</td> ".
         "<td>{$row[&#39;runoob_title&#39;]} </td> ".
         "<td>{$row[&#39;runoob_author&#39;]} </td> ".
         "<td>{$row[&#39;submission_date&#39;]} </td> ".
         "</tr>";
}
echo &#39;</table>&#39;;
登入後複製
#########排序#########
$sql = &#39;SELECT runoob_id, runoob_title, 
        runoob_author, submission_date
        FROM runoob_tbl
        ORDER BY  submission_date ASC(DESC)&#39;;
升序(降序)
默认升序
登入後複製

以上是PHP操作資料庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:jianshu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板