2008/6/24 久米
| PHP手順 |   SELECT文 発行例  | 
      INSERT, UPDATE, DELETE文 発行例  | 
  
| 1. MySQLに接続する。 | $con=mysql_connect("localhost", "root", "root"); | |
| 2. データベースを選択する。 (接続が1つなら$con省略可)  | 
    $sel=mysql_select_db("kume", $con); | |
| 3. SQL文を組み立てる。 | $sql="SELECT * FROM ranking ORDER BY point DESC"; | |
| 4. SQL文を発行する。 (接続が1つなら$con省略可)  | 
    $rst=mysql_query($sql, $con); | |
| 5. 結果を処理する。 |  $row=mysql_fetch_array($rst); mysql_free_result($rst);  | 
    //戻り値を if($rst) でチェック | 
| 6. MySQLとの接続を閉じる (自動切断するので省略可能)  | 
    $cls=mysql_close($con); | |
CREATE TABLE ranking(
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(64),
artist VARCHAR(64),
point INT
);
| id | title | artist | point | 
| Moments | 浜崎あゆみ | 980 | |
| No way to say | 浜崎あゆみ | 1230 | |
| Voyage | 浜崎あゆみ | 1100 | |
| 誰かの願いが叶うころ | 宇多田ひかる | 890 | |
| COLORS | 宇多田ひかる | 1310 | |
| ・・・ | 
| SELECT文 その1 while型  | 
    
<?php
$DBSERVER="localhost";
$DBUSER="root";
$DBPASSWORD="root";
$DBNAME="kume";
$con=mysql_connect($DBSERVER, $DBUSER, $DBPASSWORD);
$sel=mysql_select_db($DBNAME, $con);
$sql="SELECT * FROM ranking ORDER BY point DESC";
$rst=mysql_query($sql, $con);
$m="id title artist point<br>";
while($row=mysql_fetch_array($rst)){
    $m.=$row["id"]." ";
    $m.=$row["title"]." ";
    $m.=$row["artist"]." ";
    $m.=$row["point"]."<br>";
}
mysql_free_result($rst);
$cls=mysql_close($con);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>select文その1</title>
</head>
<body>
<?php print $m; ?>
</body>
</html>
 | 
  
| SELECT文 その2 for型  | 
    
<?php
$DBSERVER="localhost";
$DBUSER="root";
$DBPASSWORD="root";
$DBNAME="kume";
$con=mysql_connect($DBSERVER, $DBUSER, $DBPASSWORD);
$sel=mysql_select_db($DBNAME, $con);
$sql="SELECT * FROM ranking ORDER BY point DESC";
$rst=mysql_query($sql, $con);
$m="id title artist point<br>";
$len=mysql_num_rows($rst);
for($i=0;$i<$len;$i++){
    $row=mysql_fetch_array($rst);
    $m.=$row["id"]." ";
    $m.=$row["title"]." ";
    $m.=$row["artist"]." ";
    $m.=$row["point"]."<br>";
}
mysql_free_result($rst);
$cls=mysql_close($con);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>select文その2</title>
</head>
<body>
<?php print $m; ?>
</body>
</html>
 | 
  
| SELECT文 その3 列名知らない型  | 
    
<?php
$DBSERVER="localhost";
$DBUSER="root";
$DBPASSWORD="root";
$DBNAME="kume";
$con=mysql_connect($DBSERVER, $DBUSER, $DBPASSWORD);
$sel=mysql_select_db($DBNAME, $con);
$sql="SELECT * FROM ranking ORDER BY point DESC";
$rst=mysql_query($sql, $con);
$m="";
$fldlen=mysql_num_fields($rst);
for($i=0;$i<$fldlen;$i++){
    $m.=mysql_field_name($rst,$i)." ";
}
$m.="<br>";
while($row=mysql_fetch_array($rst)){
    for($i=0;$i<$fldlen;$i++){
        $m.=$row[$i]." ";
    }
    $m.="<br>";
}
mysql_free_result($rst);
$cls=mysql_close($con);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>select文</title>
</head>
<body>
<?php print $m; ?>
</body>
</html>
 | 
  
| INSERT文 | 
<?php
$DBSERVER="localhost";
$DBUSER="root";
$DBPASSWORD="root";
$DBNAME="kume";
$con=mysql_connect($DBSERVER, $DBUSER, $DBPASSWORD);
$sel=mysql_select_db($DBNAME, $con);
$sql="INSERT INTO ranking (title, artist, point)
        VALUES ('亜麻色の髪の乙女', '島谷ひとみ', 950)";
$rst=mysql_query($sql, $con);
if($rst) $m="成功しました。";
else $m="失敗しました。";
$cls=mysql_close($con);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>insert文</title>
</head>
<body>
<?php print $m; ?>
</body>
</html>
 | 
  
| UPDATE文 | 
<?php
$DBSERVER="localhost";
$DBUSER="root";
$DBPASSWORD="root";
$DBNAME="kume";
$con=mysql_connect($DBSERVER, $DBUSER, $DBPASSWORD);
$sel=mysql_select_db($DBNAME, $con);
$sql="UPDATE ranking SET point=1980
        WHERE title='亜麻色の髪の乙女'";
$rst=mysql_query($sql, $con);
if($rst) $m="成功しました。";
else $m="失敗しました。";
$cls=mysql_close($con);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>update文</title>
</head>
<body>
<?php print $m; ?>
</body>
</html>
 | 
  
| DELETE文 | 
<?php
$DBSERVER="localhost";
$DBUSER="root";
$DBPASSWORD="root";
$DBNAME="kume";
$con=mysql_connect($DBSERVER, $DBUSER, $DBPASSWORD);
$sel=mysql_select_db($DBNAME, $con);
$sql="DELETE FROM ranking
        WHERE title='亜麻色の髪の乙女'";
$rst=mysql_query($sql, $con);
if($rst) $m="成功しました。";
else $m="失敗しました。";
$cls=mysql_close($con);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>delete文</title>
</head>
<body>
<?php print $m; ?>
</body>
</html>
 | 
  
| 順位 | タイトル | アーティスト | ポイント | 
| 1 | COLORS | 宇多田ひかる | 1310 | 
| 2 | No way to say | 浜崎あゆみ | 1230 | 
| 3 | Voyage | 浜崎あゆみ | 1100 | 
| 4 | ・・・ | Bz | 1005 | 
| 5 | ・・・ | モー娘。 | 995 | 
| 6 | Moments | 浜崎あゆみ | 980 | 
| 7 | 誰かの願いが叶うころ | 宇多田ひかる | 890 | 
| ・・・ | 
コラム$m.="おはよう"; は $m=$m."おはよう"; の簡略形です。  |