# node-mysql
[![Build Status](https://secure.travis-ci.org/felixge/node-mysql.png?branch=v2.0)](http://travis-ci.org/felixge/node-mysql)
## Install
```bash
npm install [email protected]
```
Despite the alpha tag, this is the recommended version for new applications.
For information about the previous 0.9.x releases, visit the [v0.9 branch][].
[v0.9 branch]: https://github.com/felixge/node-mysql/tree/v0.9
## Introduction
This is a node.js driver for mysql. It is written in JavaScript, does not
require compiling, and is 100% MIT licensed.
Here is an example on how to use it:
```js
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
});
connection.connect();
connection.query('SELECT 1', function(err, rows, fields) {
if (err) throw err;
console.log('Query result: ', rows);
});
connection.end();
```
From this example, you can learn the following:
* Every method you invoke on a connection is queued and executed in sequence.
* Closing the connection is done using `end()` which makes sure all remaining
queries are executed before sending a quit packet to the mysql server.
## Contributors
Thanks goes to the people who have contributed code to this module, see the
[GitHub Contributors page][].
[GitHub Contributors page]: https://github.com/felixge/node-mysql/graphs/contributors
Additionally I'd like to thank the following people:
* [Andrey Hristov][] (Oracle) - for helping me with protocol questions.
* [Ulf Wendel][] (Oracle) - for helping me with protocol questions.
[Ulf Wendel]: http://blog.ulf-wendel.de/
[Andrey Hristov]: http://andrey.hristov.com/
## Sponsors
The following companies have supported this project financially, allowing me to
spend more time on it (ordered by time of contribution):
* [Transloadit](http://transloadit.com) (my startup, we do file uploading &
video encoding as a service, check it out)
* [Joyent](http://www.joyent.com/)
* [pinkbike.com](http://pinkbike.com/)
* [Holiday Extras](http://www.holidayextras.co.uk/) (they are [hiring](http://join.holidayextras.co.uk/vacancy/senior-web-technologist/))
* [Newscope](http://newscope.com/) (they are [hiring](http://www.newscope.com/stellenangebote))
If you are interested in sponsoring a day or more of my time, please
[get in touch][].
[get in touch]: http://felixge.de/consulting
## Community
So far all community activity has happened via the GitHub Issue system, however
additionally I have just started a mailing list and IRC channel where people
can ask questions and discuss things:
* **Mailing list**: https://groups.google.com/forum/#!forum/node-mysql
* **IRC Channel**: #node-mysql (on freenode.net)
## Establishing connections
The recommended way to establish a connection is this:
```js
var mysql = require('mysql');
var connection = mysql.createConnection({
host : String, // defaults to 'localhost'
port : Number, // defaults to 3306
socketPath : String, // defaults to undefined
user : String, // defaults to undefined
password : String, // defaults to undefined
database : String, // defaults to undefined
charset : String, // defaults to 'UTF8_GENERAL_CI'
insecureAuth : Boolean, // defaults to false
typeCast : Boolean, // defaults to true
debug : Boolean, // defaults to false
});
connection.connect(function(err) {
// connected! (unless `err` is set)
});
```
However, a connection can also be implicitly established by invoking a query:
```js
var mysql = require('mysql');
var connection = mysql.createConnection(...);
connection.query('SELECT 1', function(err, rows) {
// connected! (unless `err` is set)
});
```
Depending on how you like to handle your errors, either method may be
appropriate. Any type of connection error (handshake or network) is considered
a fatal error, see the [Error Handling](#error-handling) section for more
information.
## Terminating connections
There are two ways to end a connection. Terminating a connection gracefully is
done by calling the `end()` method:
```js
connection.end(function(err) {
// The connection is terminated now
});
```
This will make sure all previously enqueued queries are still before sending a
`COM_QUIT` packet to the MySQL server. If a fatal error occurs before the
`COM_QUIT` packet can be sent, an `err` argument will be provided to the
callback, but the connection will be terminated regardless of that.
An alternative way to end the connection is to call the `destroy()` method.
This will cause an immediate termination of the underlaying socket.
Additionally `destroy()` guarantees that no more events or callbacks will be
triggered for the connection.
```js
connection.destroy();
```
Unlike `end()` the `destroy()` method does not take a callback argument.
## Server disconnects
You may loose the connection to a MySQL server due to network problems, the
server timing you out, or the server crashing. All of these events are
considered fatal errors, and will have the `err.code =
'PROTOCOL_CONNECTION_LOST'`. See the [Error Handling](#error-handling) section
for more information.
The best way to be notified about a connection termination is to listen for the
`'close'` event:
```js
connection.on('close', function(err) {
if (err) {
// We did not expect this connection to terminate
connection = mysql.createConnection(connection.config);
} else {
// We expected this to happen, end() was called.
}
});
```
As you can see in the example above, re-connecting a connection is done by
establishing a new connection. Once terminated, an existing connection object
cannot be re-connected by design.
Please note that you will also receive a `'close'` event with an `err` argument
when a connection attempt fails because of bad credentials. If you find this
cumbersome to work with, please post to the node-mysql mailing list to discuss
improvements.
## Escaping query values
In order to avoid SQL Injection attacks, you should always escape any user
provided data before using it inside a SQL query. You can do so using the
`connection.escape()` method:
```js
var userId = 'some user provided value';
var sql = 'SELECT * FROM users WHERE id = ' + connection.escape(userId);
connection.query(sql, function(err, results) {
// ...
});
```
Alternatively, you can use `?` characters as placeholders for values you would
like to have escaped like this:
```js
connection.query('SELECT * FROM users WHERE id = ?', [userId], function(err, results) {
// ...
});
```
This looks similar to prepared statements in MySQL, however it really just uses
the same `connection.escape()` method internally.
Different value types are escaped differently, here is how:
* Numbers are left untouched
* Booleans are converted to `true` / `false` strings
* Date objects are converted to `'YYYY-mm-dd HH:ii:ss'` strings
* Buffers are converted to hex strings, e.g. `X'0fa5'`
* Strings are safely escaped
* Arrays are turned into list, e.g. ['a', 'b'] turns into `'a', 'b'`
* Objects are turned into `key = 'val'` pairs. Nested objects are cast to
strings.
* `undefined` / `null` are converted to `NULL`
* `NaN` / `Infinity` are left as-is. MySQL does not support these, and trying
to insert them as values will trigger MySQL errors until they implement
support.
If you paid attention, you may have noticed that this escaping allows you
to do neat things like this:
```js
var post = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
// Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
```
## Getting the id of an inserted row
If you are inserting a row into a table with an auto increment primary key, you
can retrieve the insert id like this:
```js
connection.query('INSERT INTO posts SET ?', {title: 'test'}, function(err, result) {
if (err) throw err;
没有合适的资源?快使用搜索试试~ 我知道了~
基于PHP的新丽都娱乐Shi时彩系统php版源码.zip
共1270个文件
js:364个
php:305个
png:202个
1 下载量 48 浏览量
2024-01-04
23:21:33
上传
评论
收藏 63.25MB ZIP 举报
温馨提示
基于PHP的新丽都娱乐Shi时彩系统php版源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于PHP的新丽都娱乐Shi时彩系统php版源码.zip (1270个子文件)
0aeb95a370a56e8885174dd177e4d503 110KB
269056f41c496a4eaf632a525bfd021f 442B
40e20e3e97e5787a332a1b2345c2f6f1 110KB
ac0f3b72de6d4baf5b46d683d22d8c0d 442B
parse-calc-count.js.bak 25KB
parse-calc-count.js.bak 25KB
parse-calc-count.js.bak 21KB
client.js.bak 10KB
client.js.bak 10KB
client.js.bak 10KB
User.class.php.bak 5KB
获取开奖.bat 12B
获取开奖.bat 12B
获取开奖.bat 12B
kj.bat 7B
kj.bat 7B
kj.bat 7B
libmysql_password.c 4KB
libmysql_password.c 4KB
libmysql_password.c 4KB
DBAccess.class 11KB
Object.class 5KB
Xxtea.class 3KB
CImage.class 2KB
wjstyle.css 40KB
jquery-ui-1.8.21.custom.css 33KB
jquery-ui-1.8.23.custom.css 32KB
jquery-ui-1.8.23.custom.css 27KB
wj43.css 26KB
help.cssv530.css 25KB
layout.css 21KB
skins.css 17KB
bl_ococ.css 13KB
jquery.datetimepicker.css 10KB
ads.css 5KB
home.css 4KB
login.css 2KB
down.css 1KB
bl_stand.css 1KB
standard.css 926B
ie.css 619B
data.csv 62B
insert-select.php.enc 834B
insert-select.php.enc 834B
parse.php.enc 626B
json.php.enc 626B
parse.php.enc 626B
json.php.enc 626B
ssc_admin_log.frm 69KB
ssc_bets.frm 11KB
ssc_members.frm 10KB
ssc_coin_log.frm 9KB
ssc_played.frm 9KB
ssc_member_recharge.frm 9KB
ssc_score_goods.frm 9KB
ssc_type.frm 9KB
ssc_member_session.frm 9KB
ssc_member_cash.frm 9KB
ssc_score_swap.frm 9KB
ssc_links.frm 9KB
ssc_member_bank.frm 9KB
ssc_count.frm 9KB
ssc_played_group.frm 9KB
ssc_data.frm 9KB
ssc_data_time.frm 9KB
ssc_order.frm 9KB
ssc_content.frm 9KB
ssc_member_level.frm 9KB
ssc_bank_list.frm 9KB
ssc_params_fandianset.frm 8KB
ssc_params.frm 8KB
ldsj_fcoin_bet.frm 1KB
ssc_fcoin_bet.frm 1KB
ldsj_fcoin_znz.frm 1KB
ssc_fcoin_znz.frm 1KB
ldsj_fcoin_cash.frm 1KB
ssc_fcoin_cash.frm 1KB
1ogo.gif 32KB
1ogo.gif 32KB
1ogo.gif 32KB
1ogo.gif 32KB
1ogo.gif 32KB
1ogo.gif 32KB
login-content-bg.gif 20KB
ts.gif 16KB
login-wel.gif 9KB
logo_unionpay.gif 7KB
250x250.gif 5KB
250x250.gif 5KB
250x250.gif 5KB
250x250.gif 5KB
250x250.gif 5KB
250x250.gif 5KB
top.gif 5KB
940x60.gif 5KB
940x60.gif 5KB
940x60.gif 5KB
940x60.gif 5KB
940x60.gif 5KB
940x60.gif 5KB
共 1270 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
资源评论
快乐无限出发
- 粉丝: 1133
- 资源: 7269
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- lo - 迭代切片、地图、通道Go 1.18+ 泛型的 Lodash 风格的 Go 库
- 一个高分辨率红外分子光谱的快速逐行代码,使用radis实现
- springboot-06-security security的页面模板(06).zip
- ILI9431台湾奕力显示芯片中文数据手册(机器翻译)
- ILI9488台湾奕力显示芯片中文数据手册(机器翻译)
- opengauss cdc
- mariadb驱动,kettle连接mariadb驱动
- VMware workstation 17 pro个人免费版
- MT3405-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- node-jenkins-api库研究.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功