標籤:
<body><?php//物件導向的方式訪問資料庫//造對象$db = new MySQLi("localhost","root","123","mydb");//判斷串連是否出錯/*if(mysqli_connect_error()){ echo "串連失敗!"; exit;}*/!mysqli_connect_error() or die("串連失敗!");//寫SQL語句$sql = "insert into Info values(‘p010‘,‘張三‘,true,‘n002‘,‘1988-2-3‘)";//執行SQL語句,查詢語句返回結果集對象,其他語句返回true或false$result = $db->query($sql);//從結果集對象中讀取資料//$attr = $result->fetch_all(MYSQLI_BOTH); //以二維數組的方式返回所有資料//$attr = $result->fetch_array(); //返回當前指標指向的這條資料//$attr = $result->fetch_assoc(); //返回當前指標指向的這條資料(關聯陣列)//$attr = $result->fetch_object(); //返回對象//$attr = $result->fetch_row(); //返回索引數組//var_dump($attr);/*while($attr = $result->fetch_row()){ var_dump($attr);}*///var_dump($result);?></body>
View Code
例子
登陸介面
<body><h1>登入</h1><form action="dlchuli.php" method="post"><div>使用者名稱:<input type="text" name="uid" /></div><div>密碼:<input type="text" name="pwd" /></div><input type="submit" value="登入" /></form></body>
View Code
<?php$uid = $_POST["uid"];$pwd = $_POST["pwd"];//造對象$db = new MySQLi("localhost","root","123","mydb");//判斷是否出錯!mysqli_connect_error() or die("串連失敗!");//寫SQL語句//容易被SQL注入攻擊,不安全//$sql = "select count(*) from Users where Uid = ‘{$uid}‘ and Pwd=‘{$pwd}‘";$sql = "select Pwd from Users where Uid=‘{$uid}‘";//執行SQL語句$result = $db->query($sql);//取資料$attr = $result->fetch_row();if($attr[0] == $pwd){ header("location:main.php");}else{ echo "登入失敗!";
View Code
註冊介面
<body><form action="zcchuli.php" method="post"><div>使用者名稱:<input type="text" name="uid" /></div><div>密碼:<input type="text" name="pwd" /></div><div>姓名:<input type="text" name="name" /></div><div>性別:<input type="radio" value="1" name="sex" />男 <input type="radio" value="0" name="sex" />女</div><div>生日:<input type="text" name="birthday" /></div><input type="submit" value="註冊" /></form></body>
View Code
<?php$uid = $_POST["uid"];$pwd = $_POST["pwd"];$name = $_POST["name"];$sex = $_POST["sex"];$birthday = $_POST["birthday"];$s = $sex==1?true:false;//造對象$db = new MySQLi("localhost","root","123","mydb");//判斷是否出錯!mysqli_connect_error() or die("串連失敗!");//寫SQL語句$sql = "insert into Users values(‘{$uid}‘,‘{$pwd}‘,‘{$name}‘,{$s},‘{$birthday}‘,0)";//執行SQL語句$result = $db->query($sql);if($result){ header("location:zhuce.php");}else{ echo "註冊失敗!";}
View Code
php 物件導向的方式訪問資料庫