php操作mysql(数据库常规操作)
php操作数据库八步走
<?php//1、建立连接$connection
= mysqli_connect("127.0.0.1", "root", "123456");//2、判断连接是否成功if (mysqli_connect_error() !=null) {die(mysqli_connect_error());
}
else {echo "连接成功!";
}
///3、选择数据库mysqli_select_db($connection,
"test");///4、设置字符集mysqli_set_charset($connection,
"utf8");///5、书写 SQL 语句$sql
= "SELECT*FROM `user` LIMIT 1";///6、执行 SQL 语句$result
= mysqli_query($connection, $sql);///7、解析结果集$array
= mysqli_fetch_array($result, MYSQLI_BOTH);///8、关闭连接mysqli_close($connection);
添加、更新、删除数据
<?php// 连接数据库$link
= mysqli_connect("127.0.0.1", "root", "123456");// 判断数据库连接是否成功if (!is_null(mysqli_connect_error())) {die(mysqli_connect_error());
}
// 选择数据库mysqli_select_db($link,
"test");// 设置连接字符集mysqli_set_charset($link,
"utf8");// 书写 SQL 语句// $sql = "INSERTINTO `user` (`username`, `password`) VALUES("user-5", "password-5")";//增加数据// $sql = "UPDATEuserSET password="password"WHERE id >4";//修改数据$sql
= "DELETEFROMuserWHERE id >4";//删除数据// 执行 SQL 语句$result
= mysqli_query($link, $sql);// 解析 SQL 语句执行结果if ($result) {echo "执行成功!"
. " 成功删除了数据 " . mysqli_affected_rows($link) . " 条!";
// . " 插入数据的 ID 是 " . mysqli_insert_id($link) . "。";}
else {echo "执行失败!";
}
// 关闭连接,释放资源mysqli_close($link);
简单查询和数据解析
<?php// 建立连接$link
= mysqli_connect("127.0.0.1", "root", "123456");// 判断连接是否成功if (mysqli_connect_error() !=null) {die(mysqli_connect_error());
}
// 选择数据库mysqli_select_db($link,
"test");// 设置字符集mysqli_set_charset($link,
"utf8");// 书写 SQL 语句$sql
= "SELECT good, class, price, stock FROM good WHERE id <4";// 执行 SQL 语句$result
= mysqli_query($link, $sql);// 解析结果集echo "
<pre>";$data
= mysqli_fetch_array($result);var_dump($data);
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {var_dump($row);
//使用或输出所有查询的数据}
while ($row = mysqli_fetch_row($result)) {var_dump($row);
//把查询结果返回到一个数组中,用数字索引取值}
while ($row = mysqli_fetch_assoc($result)) {var_dump($row);
//把查询结果返回到一个数组中,用关键字索引取值}
while ($row = mysqli_fetch_object($result)) {var_dump($row);
//从结果集中取得当前行,并作为对象返回}
$data
= mysqli_fetch_all($result, MYSQLI_BOTH);//从结果集中取得所有行作为关联数组var_dump($data);
$num
= mysqli_num_rows($result);echo "共查询到 " . $num . " 条数据!";
$num
= mysqli_num_fields($result);echo "共查询到 " . $num . " 列数据!";
// 关闭连接mysqli_close($link);
复杂查询
<?php// 建立连接$link
= mysqli_connect("127.0.0.1", "root", "123456");// 判断连接是否成功if (mysqli_connect_error() !=null) {die(mysqli_connect_error());
}
// 选择数据库mysqli_select_db($link,
"test");// 设置字符集mysqli_set_charset($link,
"utf8");// 书写 SQL 语句$sql
= "SELECT good.*, class.class FROM good, class WHERE good.class = class.id AND good.stock >=25AND good.price <=50AND good.class =4ORDERBY good.price ASC LIMIT 6";// 执行 SQL 语句$result
= mysqli_query($link, $sql);// 解析结果集echo "
<table border="1">";echo "
<tr>";echo "
<th>ID</th>";echo "
<th>商品</th>";echo "
<th>类别</th>";echo "
<th>单价</th>";echo "
<th>库存</th>";echo "
<th>更新时间</th>";echo "
<th>创建时间</th>";echo "
</tr>";while ($row = mysqli_fetch_assoc($result)) {echo "
<tr>";echo "
<td width="100px">" . $row["id"] . "</td>";echo "
<td width="100px">" . $row["good"] . "</td>";echo "
<td width="100px">" . $row["class"] . "</td>";echo "
<td width="100px">" . $row["price"] . "</td>";echo "
<td width="100px">" . $row["stock"] . "</td>";echo "
<td width="100px">" . $row["updated_at"] . "</td>";echo "
<td width="100px">" . $row["created_at"] . "</td>";echo "
</tr>";}
echo "
</table>";// 关闭连接mysqli_close($link);
程序错误排查
<?php// 建立连接$link
= mysqli_connect("127.0.0.1", "root", "123456");// 判断连接是否成功if (mysqli_connect_error() !=null) {die(mysqli_connect_error());
}
// 选择数据库mysqli_select_db($link,
"test");// 设置字符集mysqli_set_charset($link,
"utf8");// 书写 SQL 语句// $sql = "SELECT*FROM good ORDERBY price ASCGROUPBY class";$sql
= "INSERTINTO good (good, class, price, stock) VALUES("test", "test", 20, 56)";// $sql = "INSERTINTO class(class) VALUES("水果")";// 执行 SQL 语句$result
= mysqli_query($link, $sql);// 解析结果集// var_dump($result);$error
= mysqli_error($link);$errno
= mysqli_errno($link);echo "SQL 语句执行失败!错误代码:" . $errno . ",错误信息:" . $error;
// 关闭连接mysqli_close($link);
以上是 php操作mysql(数据库常规操作) 的全部内容, 来源链接: utcz.com/z/532015.html