php 物件導向的方式訪問資料庫

來源:互聯網
上載者:User

標籤:

<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 物件導向的方式訪問資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.