1. 操作MySQL
<?php
$conn = mysql_connect(‘localhost‘,‘root‘,‘‘);
if($conn){
//echo ‘connect success‘;.
mysql_select_db(‘test‘,$conn);
// $result = mysql_query("SELECT id, name FROM users WHERE id = 1");
// $result_arr = mysql_fetch_assoc($result); //mysql_fecth_array()
// print_r($result_arr);
//echo ‘data length: ‘.mysql_num_rows($result);
// $data_count = mysql_num_rows($result);
// for($i=0;$i<$data_count;$i++){
// print_r(mysql_fetch_assoc($result));
// }
$result = mysql_query("SELECT COUNT(*) FROM users WHERE name = ‘iwen‘");
if($result){
$result_arr = mysql_fetch_array($result);
echo ‘data length: ‘.$result_arr[0];
}else{
echo ‘select fail‘;
}
}else{
echo ‘connect fail‘;
}
2. 用户数据查询
<!-- <?php
require_once ‘functions.php‘;
?>-->
<!DOCTPYE html>
<html>
<head>
<meta charset="UTF-8">
<title>allusers</title>
</head>
<body>
<table style=‘text-align: left;‘>
<tr><th>id</th><th>name</th><th>age</th></tr>
<?php
$conn = mysql_connect(‘localhost‘,‘root‘,‘‘);
mysql_select_db(‘test‘);
$result = mysql_query("SELECT * FROM users ORDER BY id ASC");
$dataCount = mysql_num_rows($result);
for($i=0;$i<$dataCount;$i++){
$result_arr = mysql_fetch_assoc($result);
$id = $result_arr[‘id‘];
$name = $result_arr[‘name‘];
$age = $result_arr[‘age‘];
echo "<tr><td>$id</td><td>$name</td><td>$age</td></tr>";
}
?>
</table>
</body>
</html>
3. 用户数据添加和防注入性攻击
<form action="adduser.php" method="post">
<input type="text" name="name">
<input type="text" name="age">
<input type="submit" value="submit content">
</form>
<?php
if(!isset($_POST[‘name‘])){
die(‘user name not define‘);
}
if(!isset($_POST[‘age‘])){
die(‘user age not define‘);
}
$name = $_POST[‘name‘];
if(empty($name)){
die(‘user name is empty‘);
}
$age = $_POST[‘age‘];
if(empty($age)){
die(‘user age is empty‘);
}
$conn = mysql_connect(‘localhost‘,‘root‘,‘‘);
mysql_select_db(‘test‘);
if(!$conn){
die(‘Can not connect db‘);
}
$age = intval($age);
mysql_query("INSERT INTO users(name,age) VALUES (‘$name‘,$age)");
if(mysql_error()){
echo mysql_error();
}else{
header("Location:allusers.php");
}
4. 用户数据修改
5. 用户数据删除