连接数据库
$servername = “localhost”;
$user=”root”;
$password=”11111”;
$dbname=”news”;
$conn = new
mysqli($serverename,$user,$password,$dbname);
?>
查询数据库
$id = $_GET[‘id’];//从地址栏获取值例如http:/localhost/index.php?id=1;
$sql = “SELECTED * FROM news WHERE
id=’$id’”;
$result = $conn->query($sql);
// fetch_assoc返回根据从结果集取得的行生成的关联数组,如果没有更多行,则返回 false
while($row = $result->fetch_assoc())
{
?>
$row[‘id’]
}
?>
添加数据
$id = $_POST[‘id’];
$name = $_POST[‘name’];
$sql=”INSER INTO news (id ,name,…) VALUES
($id,’$name’,…);
$conn->query($sql);
修改数据
$name = $_POST[‘name’];
$sql = “UPDATE news SET
id=$_POST[‘id’],name=’$name’ WHERE id=$_POST[‘id’]”;
删除数据
$sql = “DELETE FROM news WHERE id=”.$_POST[‘id’];
或者
$id = $_POST[‘id’];
$sql = “DELETE FROM news WHERE id=$id”;