PHP数据库相关知识

建立连接:
$host = “localhost”;
$user = “zhou”;
$password = “111”;
$link = mysql_connect($host,$user,$password);
选择操作数据库:
mysql_select_db(“zhou”);
设置字符编码:
mysql_query(“set names ‘utf8′”);

从$link连接中查询数据:
$res = mysql_query(‘select * from user limit 1′,$link);//执行查询语句
$row = mysql_fetch_array($res);//获取查询结果集中的数据

插入数据:
$sql = “insert into user(name, age, class) values(‘$name’, ‘$age’, ‘$class’)”;
mysql_query($sql); //执行插入语句
$uid = mysql_insert_id();//得到自增的主键id

查询分页数据:
$page = 2;//当前页
$n = 2;//一页多少数据
$m = ($page – 1) * $n;
$sql = “select * from user limit $m, $n”;
$result = mysql_query($sql);
//循环获取当前页的数据
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}

更新数据:
$sql = “update user set name = ‘曹操’ where id=2 limit 1″;
if (mysql_query($sql)) {
echo ‘更新成功';
}

删除数据:
$sql = “delete from user where id=2 limit 1″;
if (mysql_query($sql)) {
echo ‘删除成功';
}

关闭链接:
mysql_close($link);

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>