Home » Docs » Server »
 

MySQL

(since 1.0)



A mysql connection class

Description


MySQL

Ape.MySQL is a class constructor. You can connect and use a MySQL database.

Parameters


host
Mysql host, can be an ip:port or an unix socket.

username
Mysql username

password
Mysql password

database
The database to select

Examples


Database connection
var sql = new Ape.MySQL("ip:port", "user", "password", "database");
 
sql.onConnect = function() {
    Ape.log('Connected to mysql server');
}
 
 

Select request
sql.query("SELECT * FROM table", function(res, errorNo) {
    if (errorNo) Ape.log('Request error : ' + errorNo + ' : '+ this.errorString()); 
    else {
        Ape.log('Fetching ' + res.length);
        res.each(function(data) {
            Ape.log(data.content);//data.<column name>
        }); 
    }
});

Insert request
sql.query("INSERT INTO table VALUES('a','b','c')", function(res, errorNo) {
    if (errorNo) Ape.log('Request error : ' + errorNo + ' : '+ this.errorString()); 
    else Ape.log('Inserted');
});

User notes


Kasper Holtze
Kasper Holtze said:
"Mysql host, can be an ip, an hostname or an unix socket."

That is currently wrong. It can not be a hostname, it should be an IP address.
02/02/10 16:20

efyx
efyx said:
This wiki page gives you many information on how to use MySQL module

<a href="http://www.ape-project.org/wiki/index.php/Serverside_js_module_using_MySQL" rel="nofollow" target="_blank">http://www.ape-project.org/wiki/index.php/Serverside_js_module_using_MySQL</a>
01/04/10 16:22

Post a note


User notes may be edited or deleted, and usually a note is deleted because of the following reasons:
  • Bugs: Instead report a bug
  • Missing documentation: report that as a bug.
  • Support questions: See the community for available options.

In other words, do not ask questions within the user notes.

Note <b>,<u>,<i> HTML tags are allowed in the posts and the note formatting is preserved. URLs will be turned into clickable links, JavaScipt and C code blocks enclosed in the <source="javascript">JS Code</source> and <source="c">C Code</source> tags will be source highlighted automatically.