这是我学习链接数据库写的代码 没写完 先放这 以后在补充
<?php //链接sql class db{ //基本类 public $con; public static $sname="127.0.0.1"; public static $uname="root"; public static $pword=""; //静态变量 public function __construct(){ //构造函数先执行 不能为静态 $this->con = mysql_connect(self::$sname,self::$uname,self::$pword); if(!$this->con){ die("cound no connect:".mysql_error()); } } public function o($sql){ mysql_select_db("L_db",$this->con); mysql_query($sql,$this->con); } function __destruct(){ //这是个析构函数, 在对象销毁前调用 mysql_close($this->con); } } class cr extends db { //派生 创建类 function create(){ //创建 database if (mysql_query("CREATE DATABASE L_db",$this->con)){ echo "Database created"; }else{ echo "Error creating database".mysql_error(); } } function create_tabe(){ //创建表 mysql_select_db("L_db", $this->con); //选着数据库 $sql = "CREATE TABLE PCR( FilstName varchar(15), LastName varchar(16), Age int )"; mysql_query($sql,$this->con); } } class rw extends db { function w($f,$l,$a){ $sql="INSERT INTO PCF(FilstName,LastName,Age) VALUES( ‘$f‘,‘$l‘,‘$a‘)"; db::o($sql); } function rall (){ $sql="SELECT * FROM PCF"; var_dump(db::o($sql)); $sql=db::o($sql); while($row=mysql_fetch_array($sql)){ echo $row[‘FilstName‘]." ".$row[‘LastName‘]; echo "<br />"; } } } $f[email protected]$_POST[‘firstname‘]; $l[email protected]$_POST[‘lastname‘]; $a[email protected]$_POST[‘age‘]; var_dump($a,$l,$f); $rw=new rw(); $rw->w($f,$l,$a); $rw->rall(); //创建数据库 //$cdb=new cr; //$cdb->created(); 创建数据库 //$cdb->create_tabe(); //创建数据表 //PCRID int NOT NULL AUTO_INCREMENT, //PCRID 不能为空 设置为自动增加 //PRIMARY KEY(PCRID), //FilstName varchar(15), //varchar 类型 (15)最大长度 $sql1="CREATE TABLE PCM( PCRID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(PCRID), FilstName varchar(15), LastName varchar(15), Age int )"; $sql2="CREATE TABLE PCF( PCRID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(PCRID), FilstName varchar(15), LastName varchar(15), Age int )"; $sql3="INSERT INTO PCF( FilstName,LastName,Age) VALUES (‘Glenn‘,‘Query‘,‘33‘)"; //$srw=new srw; //$srw->w($sql3); //db::o($sql2); ?>
时间: 2024-09-28 21:47:12