PHP資料庫的基本操作

來源:互聯網
上載者:User

標籤:更新   rds   varchar   auto   tab   ast   use   primary   語句   

<?php

//串連資料庫
/*$con=mysql_connect("localhost","root","19960621");// or die(‘Error:‘.mysql_error());
if(!$con){
die("串連失敗!".mysql_errno());
}else{
echo "串連資料庫成功";
mysql_query("CREATE DATABASE dbmysql",$con);
mysql_select_db("dbmysql",$con);
$sql="CREATE TABLE person (personID int NOT NULL AUTO_INCREMENT,FirstName varchar(15),LastName varchar(15),Age int,PRIMARY KEY(personID))";
$str=mysql_query($sql,$con);
echo "<br>".$str."<br>";
mysql_close($con);
}
//往表裡插資料
$con=mysql_connect("localhost","root","19960621");
mysql_select_db("dbmysql",$con);
mysql_query("insert into person(`FirstName`, `LastName`, `Age`)values(‘tom‘,‘tony‘,‘20‘)");
mysql_query("insert into person(`FirstName`, `LastName`, `Age`)values(‘lh‘,‘h‘,‘16‘)");
mysql_query("insert into person(`FirstName`, `LastName`, `Age`)values(‘jy‘,‘j‘,‘24‘)");
//查詢資料
$con=mysql_connect("localhost","root","19960621");
mysql_select_db("dbmysql",$con);
$result=mysql_query("select * from person");
while($row=mysql_fetch_array($result)){
echo $row["FirstName"]."".$row["LastName"];
echo "<br>";
}
//更新資料
$con=mysql_connect("localhost","root","19960621");
mysql_select_db("dbmysql",$con);
mysql_query("update person set FirstName=‘dx‘,LastName=‘x‘,`Age`=18 where `personID`=3");
//刪除資料
$con=mysql_connect("localhost","root","19960621");
mysql_select_db("dbmysql",$con);
mysql_query("delete from person where `personID`=2");
mysql_close($con);

//mysql_list_dbs()函數==>列出 MySQL 伺服器中所有的資料庫
$con=mysql_connect("localhost","root","19960621");
$dl=mysql_list_dbs($con);
while($db=mysql_fetch_object($dl)){
echo $db->Database."<br>";
}
mysql_close($con);
//mysql_num_fields()函數==>返回結果集中欄位的數
$con = mysql_connect("localhost", "root", "19960621");
if (!$con){
die(‘Could not connect: ‘ . mysql_error());
}
$db_selected = mysql_select_db("dbmysql",$con);
$sql = "SELECT * FROM person";
$result = mysql_query($sql,$con);
echo mysql_num_fields($result);//輸出資料行的個數
mysql_close($con);
//mysql_num_rows()函數==>返回結果集中行的數目
$con = mysql_connect("localhost", "root", "19960621");
if (!$con){
die(‘Could not connect: ‘ . mysql_error());
}
$db_selected = mysql_select_db("dbmysql",$con);
$sql = "SELECT * FROM person";
$result = mysql_query($sql,$con);
echo mysql_num_rows($result);//查詢出的總行數
mysql_close($con);
//mysql_affected_rows()函數==>返回前一次MySQL操作所影響的記錄行數
$con = mysql_connect("localhost", "root", "19960621");
if (!$con){
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db("dbmysql");
mysql_query("DELETE FROM person WHERE personid =3");
$rc = mysql_affected_rows();
echo "Records deleted: " . $rc;
mysql_close($con);
//mysql_real_escape_string()函數==>轉義SQL語句中使用的字串中的特殊字元
//獲得使用者名稱和密碼的代碼
$user="a\r\nbc";
$pwd="a\r\nbc";
//轉義使用者名稱,以便在 SQL 中使用
$user = mysql_real_escape_string($user);
$pwd = ($pwd);
$sql = "SELECT * FROM users WHERE user=‘" . $user . "‘ AND password=‘" . $pwd . "‘";
echo $sql; //使用者名稱和密碼對比
//mysql_result() 函數返回結果集中一個欄位的值
$con = mysql_connect("localhost", "root", "19960621");
if (!$con){
die(‘Could not connect: ‘ . mysql_error());
}
$db_selected = mysql_select_db("dbmysql", $con);
$sql = "SELECT * from Person";
$result = mysql_query($sql,$con);
echo mysql_result($result,1,2);
mysql_close($con);*/

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.