MyBB Community Forums

Full Version: $db->insert_query(). Can I have a syntax example?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to insert some values in a table, but it seems that MyBB does it differently than by the normal MySQL INSERT INTO syntax.

The default MySQL query is something like:

"INSERT INTO table_example (uid, anotherexample, `anotherexample ') VALUES ('1', 'test', 'test');"

Could I have an example of the MyBB $db->insert_query() syntax?
When you use $db->insert_query it takes two parameters. The first is the name of the table without a prefix. The second is an array in which the keys match up with the field names.

$new_user = array(
"username" => "Test User",
"usergroup" => 2,
"email" => "[email protected]"
);
$db->insert_query("users", $new_user);
Note you can still use the normal SQL code via the query() and write_query() methods.
No you should use write_query() instead of query() and that you would be better off escaping the data before insertion.

Toungue
Obviously. But I'm just pointing in the right direction. write_query should be used for inserts/updates/deletes, query should be used for selects. Otherwise the master/slave setup is useless.
You should always use insert_query/simple_select. Only queries which don't have a function (like create table) should use (write_)query. Otherwise it's likely you break pgsql/sqlite Wink
Or queries which use joins, then you have to write them manually too unfortunately.