[原創][技術]PHP學習筆記(6)–PHP與mySQL

NO IMAGE

 

MySQL Connect
MySQL Create
MySQL Insert
MySQL Select
MySQL Where
MySQL Order By
MySQL Update
MySQL Delete
PHP ODBC

———————————————————————————————–

MySQL Connect

<?php
$con = mysql_connect(“localhost”,”root”,”mysql”);
if (!$con)
  {
  die(‘mySQL資料庫不能連線: ‘ . mysql_error());
  }
else
{
 echo “mySQL連線成功!”;
}

echo “<br>”;

//關閉連線,其實如果沒有此操作,在指令碼結束後也會自動關閉連線
mysql_close($con); //看呢,$con就像是控制代碼之類的東西
echo “mySQL關閉了”;
?>

———————————————————————————————–

 MySQL Create

建立資料庫

<?php
$con = mysql_connect(“localhost”,”root”,”mysql”);
if (!$con)
  {
  die(‘mySQL資料庫不能連線: ‘ . mysql_error());
  }
else
{
 echo “mySQL連線成功!”;
}

echo “<br>”;

if (mysql_query(“CREATE DATABASE my_phpsqldb”,$con))
  {
  echo “my_phpsqldb成功建立”;
  }
else
  {
  echo “建立資料庫失敗: ” . mysql_error();
  }

echo “<br>”;

//關閉連線,其實如果沒有此操作,在指令碼結束後也會自動關閉連線
mysql_close($con); //看呢,$con就像是控制代碼之類的東西
echo “mySQL關閉了”;
?>

 執行結果:

mySQL連線成功!
my_phpsqldb成功建立

mySQL關閉了

如果再次執行,結果是:

mySQL連線成功!
建立資料庫失敗: Can’t create database ‘my_phpsqldb’; database exists
mySQL關閉了

 

建立表

 // Create table in my_phpsqldb database
mysql_select_db(“my_phpsqldb”, $con);
$sql = “CREATE TABLE person
(
FirstName varchar(15),
LastName varchar(15),
Age int
)”;
mysql_query($sql,$con);
echo “建立了CREAT TABLE person”;

 ——————————————————————————–

 MySQL Insert

例子:

1)先在index.html中放入以下程式碼

<html>
<body>

<form action=”insert.php” method=”post”>
Firstname: <input type=”text” name=”firstname” />
Lastname: <input type=”text” name=”lastname” />
Age: <input type=”text” name=”age” />
<input type=”submit” />
</form>

</body>
</html>

2)再在insert.php 放入以下程式碼

<?php
$con = mysql_connect(“localhost”,”root”,”mysql”);
if (!$con)
  {
  die(‘Could not connect: ‘ . mysql_error());
  }

mysql_select_db(“my_phpsqldb”, $con);

$sql=”INSERT INTO person (FirstName, LastName, Age)
VALUES
(‘$_POST[firstname]’,’$_POST[lastname]’,’$_POST[age]’)”;

if (!mysql_query($sql,$con))
  {
  die(‘Error: ‘ . mysql_error());
  }
echo “1 record added”;

mysql_close($con)
?>

3)執行, 新增First name 為stephen, lastname為zhu,年齡28, 提交,顯示

1 recored added.

——————————————————————————— 

 MySQL Where

 接上面的資料庫,來查詢下看一看:

<?php
$con = mysql_connect(“localhost”,”root”,”mysql”);
if (!$con)
  {
  die(‘Could not connect: ‘ . mysql_error());
  }

mysql_select_db(“my_phpsqldb”, $con);

$result = mysql_query(“SELECT * FROM person
WHERE FirstName=’Stephen'”);

while($row = mysql_fetch_array($result))
  {
  echo $row[‘FirstName’] . ” ” . $row[‘LastName’];
  echo “<br />”;
  }

?>

 ———————————————————————————

MySQL Order By

 <?php
$con = mysql_connect(“localhost”,”root”,”mysql”);
if (!$con)
  {
  die(‘Could not connect: ‘ . mysql_error());
  }

mysql_select_db(“my_phpsqldb”, $con);

$result = mysql_query(“SELECT * FROM person ORDER BY Age”);

while($row = mysql_fetch_array($result))
  {
  echo $row[‘FirstName’];
  echo ” ” . $row[‘LastName’];
  echo ” ” . $row[‘Age’];
  echo “<br />”;
  }

mysql_close($con);
?>

執行結果可能是:

Stephen Zhu 28
Andy Liu 42
Jackey Chen 46

———————————————————————————-

MySQL Update

 <?php
$con = mysql_connect(“localhost”,”root”,”mysql”);
if (!$con)
  {
  die(‘Could not connect: ‘ . mysql_error());
  }

mysql_select_db(“my_phpsqldb”, $con);

mysql_query(“UPDATE Person SET Age = ’18’
WHERE FirstName = ‘Stephen’ AND LastName = ‘Zhu'”);

mysql_close($con);
?>

執行完之後, 再檢視資料庫, 年齡已經被改成了18

———————————————————————————-

MySQL Delete

 <?php
$con = mysql_connect(“localhost”,”root”,”mysql”);
if (!$con)
  {
  die(‘Could not connect: ‘ . mysql_error());
  }

mysql_select_db(“my_phpsqldb”, $con);

mysql_query(“DELETE FROM person WHERE FirstName=’Jacky'”);

echo “Jackey DELETED!”;

mysql_close($con);
?>

這樣就把Jacky的記錄刪除掉了

———————————————————————————-

PHP ODBC

 

 ODBC是微軟的資料庫介面標準, 這裡不多提了