忍者ブログ
[605] [604] [603] [602] [601] [600] [599] [598] [597] [596] [595
カレンダー
09 2024/10 11
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
カテゴリー
最新コメント
最新トラックバック
バーコード
ブログ内検索
アクセス解析
mysqli関数を使っていたので、家に帰って、前に家でPHPでmysqlを使うプログラムを勉強していたので、インターネットや本で調べてmysqliを使ってみたら上手く行きました。

<html>
<head>
<meta http-equiv="Content-type"
content="text/html; charxet=euc-jp">
<title>MYSQLi データの表示</title>
</head>
<body>
<?php
//------------------------------------------------------------
//□ サーバに接続
//------------------------------------------------------------
$my_Con = new mysqli("localhost", "user", "password", "database")
or die("データベースとの接続に失敗しました");

$my_Con->set_charset("UTF-8");

$stmt = $my_Con->prepare("INSERT INTO friends (no, name, birth, email) VALUES (?, ?, ?, ?);")
or exit("prepare errorn");
$stmt->bind_param("isss(プリペアドステートメントで使われる変数(?の箇所の型、iは整数、sは文字列))", $no, $name, $birth, $email);
$no = 8;
$name = "テスト";
$birth = "1970-10-21";
$email = "test@test.co.jp";
$stmt->execute() or exit("bind errorn");
echo $stmt->affected_rows ."行追加されました。
";


$no = 9;
$name = "テスト";
$birth = "1970-10-22";
$email = "test@test.co.jp";
$stmt->execute() or exit("bind errorn");
echo $stmt->affected_rows ."行追加されました。
";
//------------------------------------------------------------
//□ テーブルからデータを読む
//------------------------------------------------------------
$stmt = $my_Con->prepare("SELECT * FROM friends")
or exit("prepare errorn");
$stmt->execute() or exit("bind errorn");
$stmt->bind_result($result_id_no, $result_name, $result_birth, $result_email);

while ($stmt->fetch()) {
echo $result_id_no;
echo $result_name;
echo $result_birth;
echo $result_email;
echo "<br>";
}

?>
</body>
</html>

<html>
<head>
<meta http-equiv="Content-type"
content="text/html; charxet=euc-jp">
<title>MYSQLi データの表示</title>
</head>
<body>
<?php
//------------------------------------------------------------
//□ サーバに接続
//------------------------------------------------------------
$my_Con = new mysqli("localhost", "user", "password", "database")
or die("データベースとの接続に失敗しました");

$my_Con->set_charset("UTF-8");

$stmt = $my_Con->prepare("UPDATE friends SET name = ?, birth = ?, email = ? WHERE no = ?;")
or exit("prepare errorn");
$stmt->bind_param("sssi", $name, $birth, $email, $no);
$name = "テスト変更";
$birth = "1970-10-22";
$email = "henkou@test.co.jp";
$no = 8;
$stmt->execute() or exit("bind errorn");
echo $stmt->affected_rows ."行変更されました。
";


//------------------------------------------------------------
//□ テーブルからデータを読む
//------------------------------------------------------------
$stmt = $my_Con->prepare("SELECT * FROM friends")
or exit("prepare errorn");
$stmt->execute() or exit("bind errorn");
$stmt->bind_result($result_id_no, $result_name, $result_birth, $result_email);

while ($stmt->fetch()) {
echo $result_id_no;
echo $result_name;
echo $result_birth;
echo $result_email;
echo "<br>";
}

?>
</body>
</html>

<html>
<head>
<meta http-equiv="Content-type"
content="text/html; charxet=euc-jp">
<title>MYSQLi データの表示</title>
</head>
<body>
<?php
//------------------------------------------------------------
//□ サーバに接続
//------------------------------------------------------------
$my_Con = new mysqli("localhost", "user", "password", "database")
or die("データベースとの接続に失敗しました");

$my_Con->set_charset("UTF-8");

$stmt = $my_Con->prepare("DELETE FROM friends WHERE no = ?;")
or exit("prepare errorn");
$stmt->bind_param("i", $no);
$no = 9;
$stmt->execute() or exit("bind errorn");
echo $stmt->affected_rows ."行削除されました。
";


//------------------------------------------------------------
//□ テーブルからデータを読む
//------------------------------------------------------------
$stmt = $my_Con->prepare("SELECT * FROM friends")
or exit("prepare errorn");
$stmt->execute() or exit("bind errorn");
$stmt->bind_result($result_id_no, $result_name, $result_birth, $result_email);

while ($stmt->fetch()) {
echo $result_id_no;
echo $result_name;
echo $result_birth;
echo $result_email;
echo "<br>";
}

?>
</body>
</html>

7月からの仕事のプログラムではmysqliでデータベースからデータを取り出す時にmysqli_fetch_arrayという関数を使っていますが、

(例)$res = $mysqli->query(SQL文 or die((mysql_error());
$row = mysqli_fetch_array($res, MYSQL_NUM))


die()関数が気になり、本で調べたり、インターネットで調べるとPHPスクリプトを終了すると書かれていました。
mysql_error()関数も気になり、本で調べたり、インターネットで調べると直近のMYSQLの操作からエラーメッセージを取得と書かれていました。
MYSQL_NUMも気になり、本で調べたり、インターネットで調べると「数値添字のみが 取得されます。」と書かれていました。

(例)<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}

mysql_free_result($result);
?>



mysqliの使い方(1)
http://php.studynet.jp/basic/mysql%e3%81%8b%e3%82%89mysqli%e3%81%b8%e3%81%ae%e7%bd%ae%e3%81%8d%e6%8f%9b%e3%81%88%ef%bc%88%ef%bc%91%ef%bc%89

mysqliでプリペアドステートメント
http://php-meomo.seesaa.net/article/151350420.html

プリペアドステートメントおよびストアドプロシージャ
http://php.net/manual/ja/pdo.prepared-statements.php

MySQL 改良版拡張モジュール
http://php.net/manual/ja/book.mysqli.php

PHPスクリプトを終了する( exit()、die() )
http://www.php-ref.com/basic/01_exit.html

mysql_error
http://php.net/manual/ja/function.mysql-error.php

mysql_fetch_array
http://php.net/manual/ja/function.mysql-fetch-array.php

PR
この記事にコメントする
name
title
color
mail
URL
comment
pass   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
secret (チェックを入れると管理人だけに表示できます)
この記事へのトラックバック
この記事にトラックバックする:
Powered by Ninja Blog    template by Temp* factory    icon by MiniaureType

忍者ブログ [PR]