Jun 11, 2012

PHPのMySQLライブラリーが改良されたので対応する(その2)


mysql_ から mysqli_ に変更するために、自分が利用している対象関数の仕様を確認します。

mysqli_ は、 オブジェクト指向型 と 手続き型 のインターフェースがありますが
今回は、オブジェクト指向型で統一しようと思います。

======================================================================
mysql_real_escape_string

[API比較]
string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier = NULL ] )
mysqli::real_escape_string ( string $escapestr )
----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

mysql_real_escape_string($user)
---------------------------------------------
$mysqli->real_escape_string($user)

======================================================================
mysql_query

[API比較]

resource mysql_query ( string $query [, resource $link_identifier = NULL ] )
mysqli::query ( string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )
----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
$result = mysql_query($sql_query, $link);
---------------------------------------------
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$result = $mysqli->query($sql_query);
======================================================================
mysql_error

[API比較]

string mysql_error ([ resource $link_identifier = NULL ] )
string $mysqli->error;
----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
echo mysql_errno($link)
---------------------------------------------
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
echo $mysqli->connect_error;
======================================================================
mysql_num_rows

[API比較]

int mysql_num_rows ( resource $result )
int $mysqli_stmt->num_rows;
----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
---------------------------------------------
$result = $mysqli->query($sql_query);
$num_rows = $result->num_rows;

======================================================================
mysql_fetch_assoc

[API比較]

array mysql_fetch_assoc ( resource $result )
array mysqli_result::fetch_assoc ( void )
----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

$result = mysql_query($sql_query);
while ($row = mysql_fetch_assoc($result)) {
}
---------------------------------------------
$result = $mysqli->query($sql_query)
while ($row = $result->fetch_assoc()) {
}
======================================================================
mysql_data_seek

[API比較]

bool mysql_data_seek ( resource $result , int $row_number )
bool mysqli_result::data_seek ( int $offset )

----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

$result = mysql_query('SELECT last_name, first_name FROM friends');
$row = mysql_fetch_assoc($result);
mysql_data_seek($result, $i);
---------------------------------------------
$result = $mysqli->query('SELECT last_name, first_name FROM friends');
$row = $result->fetch_assoc();
$result->data_seek(399);
======================================================================
mysql_free_result

[API比較]

bool mysql_free_result ( resource $result )
void mysqli_result::free_result ( void )
----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
mysql_free_result($result);
---------------------------------------------
$result = $mysqli->query('SELECT last_name, first_name FROM friends');
$result->free_result();

======================================================================
mysql_connect

[API比較]

resource mysql_connect ([ string $server = ini_get("mysql.default_host")
  [, string $username = ini_get("mysql.default_user")
  [, string $password = ini_get("mysql.default_password")
  [, bool $new_link = false [, int $client_flags = 0 ]]]]] )
mysqli::__construct() ([ string $host = ini_get("mysqli.default_host")
  [, string $username = ini_get("mysqli.default_user")
  [, string $passwd = ini_get("mysqli.default_pw")
  [, string $dbname = ""
  [, int $port = ini_get("mysqli.default_port")
  [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )
----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
---------------------------------------------
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

======================================================================
mysql_close

[API比較]

bool mysql_close ([ resource $link_identifier = NULL ] )
bool mysqli::close ( void )
----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_close($link);
---------------------------------------------
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$mysqli->close();
======================================================================
mysql_select_db

[API比較]

bool mysql_select_db ( string $database_name [, resource $link_identifier = NULL ] )
bool mysqli::select_db ( string $dbname )

----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$db_selected = mysql_select_db('foo', $link);
---------------------------------------------
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
$mysqli->select_db("world");
======================================================================
mysql_insert_id

[API比較]

int mysql_insert_id ([ resource $link_identifier = NULL ] )
mixed $mysqli->insert_id;
----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

mysql_query("INSERT INTO mytable (product) values ('kossu')");
$id = mysql_insert_id();
---------------------------------------------
$mysqli->query("INSERT INTO mytable (product) values ('kossu')");
$id = $mysqli->insert_id;
======================================================================
mysql_affected_rows

[API比較]

mysql_affected_rows ([ resource $link_identifier = NULL ] )
int $mysqli->affected_rows;

----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

mysql_query('DELETE FROM mytable WHERE id < 10');
printf("Records deleted: %d\n", mysql_affected_rows());
---------------------------------------------
$mysqli->query("DELETE FROM Language WHERE Percentage < 50");
printf("Affected rows (DELETE): %d\n", $mysqli->affected_rows);

======================================================================
mysql_result

[API比較]

string mysql_result ( resource $result , int $row [, mixed $field = 0 ] )
(無し)
----------------------------------------------------------------------
[利用例比較](エラー処理は省略)

$result = mysql_query('SELECT name FROM work.employee');
echo mysql_result($result, 2);
---------------------------------------------
(無し)
======================================================================

No comments:

Post a Comment