//1.连接数据库服务
$db_link = mysql_connect("localhost","news","123456") or die("连接失败," . mysql_error());
echo "连接成功!";
//2.选择要操纵的数据库
mysql_select_db("news") or die("选择数据库失败!".mysql_error());
//3.在数据库表中插入数据
$name = $_POST['name'];
$sql = "insert into class (name) values ('$name')";
$result = mysql_query($sql);//获得函数的返回值
if($result){
echo "成功!";
}else{
echo "失败!" . mysql_error();
}
?>
// 连接,选择数据库
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');
// 执行 SQL 查询
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// 用 HTML 显示结果
echo "\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t\n";
foreach ($line as $col_value) {
echo "\t\t$col_value | \n";
}
echo "\t
\n";
}
echo "
\n";
// 关闭连接
mysql_close($link);
?>
//1.连接数据库服务
$db_link = mysql_connect("localhost","news","123456") or die("连接失败," . mysql_error());
echo "连接成功!";
//2.选择要操纵的数据库
mysql_select_db("news") or die("选择数据库失败!".mysql_error());
//3.在数据库中创建一个数据库表
$sql = "create table class (id int primary key auto_increment,name char(20) not null)";
$result = mysql_query($sql);//获得函数的返回值
if($result){
echo "创建表成功!";
}else{
echo "创建表失败!" . mysql_error();
}
?>
//1.连接数据库服务
$db_link = mysql_connect("localhost","news","123456") or die("连接失败," . mysql_error());
echo "连接成功!";
//2.选择要操纵的数据库
mysql_select_db("news") or die("选择数据库失败!".mysql_error());
//3.在数据库表中插入数据
$sql = "insert into class (name) values ('sports')";
$result = mysql_query($sql);//获得函数的返回值
if($result){
echo "成功!";
}else{
echo "失败!" . mysql_error();
}
?>
//1.连接数据库服务
$db_link = mysql_connect("localhost","news","123456") or die("连接失败," . mysql_error());
echo "连接成功!";
//2.选择要操纵的数据库
mysql_select_db("news") or die("选择数据库失败!".mysql_error());
//3.在数据库表中删除数据
$sql = "delete from class where id = 19";
$result = mysql_query($sql);//获得函数的返回值
if($result){
echo "成功!";
}else{
echo "失败!" . mysql_error();
}
?>
//1.连接数据库服务
$db_link = mysql_connect("localhost","news","123456") or die("连接失败," . mysql_error());
//echo "连接成功!";
//2.选择要操纵的数据库
mysql_select_db("news") or die("选择数据库失败!".mysql_error());
//3.在数据库表中修改数据
$sql = "update class set name='army' where name = 'sports'";
$result = mysql_query($sql);//获得函数的返回值
if($result){
echo "成功!";
}else{
echo "失败!" . mysql_error();
}
?>