时间:2021-07-01 10:21:17 帮助过:2人阅读
二、删除数据库
$sql = ‘DROP DATABASE my_db‘; $retval = mysql_query( $sql, $con ); if(! $retval ) { die(‘删除数据库失败: ‘ . mysql_error()); } echo "数据库 RUNOOB 删除成功\n";
三、创建数据表
$sqler = "CREATE TABLE tableName (id int(10) unsigned primary key not null auto_increment, name varchar(32) NOT NULL default ‘0‘, age int(10) NOT NULL default ‘0‘, content text )"; mysql_query($sqler);
四、删除数据表
$sql = "DROP TABLE tableName"; $retval = mysql_query( $sql, $con ); if(! $retval ) { die(‘数据表删除失败: ‘ . mysql_error()); } echo "数据表删除成功\n";
五、增加和删除表字段
//增加字段 $sql="alter table tableName add fieldName varchar(50) not null"; $result=mysql_query($sql); //删除字段 $sql="alter table tableName drop column fieldName"; $result=mysql_query($sql);
六、插入表数据
$name = "张三"; $age = "20"; $sex = "男"; $sql = "INSERT INTO tableName ". "(name,age, sex) ". "VALUES ". "(‘$name‘,‘$age‘,‘$sex‘)"; $retval = mysql_query( $sql, $con );
七、查询数据
$sql = ‘SELECT id, name, age, sex FROM tableName‘; $retval = mysql_query( $sql, $con ); while($row = mysql_fetch_array($retval)) { echo "ID:{$row[‘id‘]} <br> ". "Name: {$row[‘name‘]} <br> ". "Age: {$row[‘age‘]} <br> ". "Sex : {$row[‘sex‘]} <br> ". "--------------------------------<br>"; }
八、修改数据
$sql = "update tableName set name =‘{$name}‘,age=‘{$age}‘ where id=‘{$id}‘"; $result = mysql_query($sql);
九、删除数据
$sql = "delete from tableName where id=‘{$_GET[‘id‘]}‘"; $result = mysql_query($sql);
十、关闭数据库
mysql_close($con);
MySQL操作
标签: