1. 程式人生 > >PHP基本操作

PHP基本操作

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);*/