你为什么不使用PDO
?
<?php
try {
// config
$dsn = 'mysql:dbname=japanesewords;host=127.0.0.1;charset=utf8';
$username = 'root';
$password = '';
$options = array(
// necessary for rowCount() on SELECT
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
// for catching SQL errors as PDOException
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
// default fetch mode is used for iterating PDOStatement by foreach()
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
);
// connect
$pdo = new PDO($dsn, $username, $password, $options);
// execute SQL
$stmt = $pdo->query('SELECT * FROM japanesedefinition');
// check row count
if (!$stmt->rowCount()) {
throw new Exception('no data');
}
// fetch results and display
echo "<p>\n";
foreach ($stmt as $row) {
printf("foo: %s; bar: %s;<br />\n", $row->foo, $row->bar);
}
echo "</p>\n";
} catch (Exception $e) {
printf("<p>%s</p>\n", $e->getMessage());
}
不知道你是不是日本人,用PHP连接MySQL的日文总结。
http://qiita.com/mpyw/items/b00b72c5c95aac573b71