Home » Docs » Server »
 

MySQL.query

(since 1.0)



Execute a mysql query

Description


string MySQL.query ( string query, function fn )

Used to execute a mysql query, the callback is called when the request ends, if it was a SELECT query, res contains an array of objects.

For now callback function is mandatory, if you don't want your request to have a callback function, use $empty as second argument

Parameters


query
The mysql query string

fn
  • result - (array) An array of objects if the query was a "SELECT" query.
  • errorNo - (number) If an error occurred the error code.

Examples


sql.query("SELECT * FROM table", function(res, errorNo) {
    if (errorNo) Ape.log('Request error : ' + errorNo + ' : '+ this.errorString()); 
    else {
        Ape.log('Fetching ' + res.length);
	for(var i = 0; i < res.length; i++) {
            Ape.log(res[i].title);//res[i].<column name>
        }); 
    }
});

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


# No comment

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.