mysql_connect在将来的php版本会被废弃,建议用mysqli或者PDO代替
通过php添加数据
header(
"Content-type:text/html;charset=utf-8");
$link = mysql_connect(
"localhost",
"root",
"123456");
mysql_select_db(
"t2");
mysql_query(
"set names utf8");
$name =
"user4";
$age =
"213";
$sqlSelect =
"insert into people(name,age) values('{$name}','{$age}'),('{$name}','{$age}')";
$result = mysql_query(
$sqlSelect);
if(
$result){
echo '添加影响多少行:'.mysql_affected_rows();
}
else{
echo '添加失败';
}
mysql_close(
$link);
通过php删除数据
header(
"Content-type:text/html;charset=utf-8");
$link = mysql_connect(
"localhost",
"root",
"123456");
mysql_select_db(
"t2");
mysql_query(
"set names utf8");
$sqlSelect =
"delete from people where id>5";
$result = mysql_query(
$sqlSelect);
if(
$result){
echo '查询影响多少行:'.mysql_affected_rows();
}
else{
echo '删除失败';
}
mysql_close(
$link);
通过php更新数据
header(
"Content-type:text/html;charset=utf-8");
$link = mysql_connect(
"localhost",
"root",
"123456");
mysql_select_db(
"t2");
mysql_query(
"set names utf8");
$sqlSelect =
"update people set name='linei2' where id=61";
$result = mysql_query(
$sqlSelect);
if(
$result){
echo '更新影响多少行:'.mysql_affected_rows();
}
else{
echo '更新失败';
}
mysql_close(
$link);
通过php查找数据
header(
"Content-type:text/html;charset=utf-8");
$link = mysql_connect(
"localhost",
"root",
"123456");
mysql_select_db(
"t2");
mysql_query(
"set names utf8");
$sqlSelect =
"select * from people";
$result = mysql_query(
$sqlSelect);
while (
$row = mysql_fetch_assoc(
$result)){
echo "{$row['id']}".
' --- '.
"{$row['name']}".
' -- '.
"{$row['age']}".
'<br/>';
}
echo '查询到的行数:'.mysql_num_rows(
$result);
mysql_free_result(
$result);
mysql_close(
$link);
转载请注明原文地址: https://ju.6miu.com/read-673520.html